Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Counting String in Specific Alphabet

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Thanked 0 Times in 0 Posts

    Default Counting String in Specific Alphabet

    Consider strings over the alphabet Σ = {a, b}, and the function count(n) that returns the
    number of strings of length n containing more a’s than b’s. For example, for n = 3, there
    are four such strings, namely, [”aaa”, ”aab”, ”aba”, ”baa”], so the count is 4. Implement the
    function in Java using recursion (Assume that you are not allowed to use any formula for the

    I appreciate any help with this assignment, I'm completely stuck. Here's where's my head is at:

    static int count(int n, int c) {
    int a = 0;
    int b = 0;

    static int count() {
    int c = 0;
    return c;

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Counting String in Specific Alphabet

    Can you compute all the Strings, including such as bbb and bba etc, and reject those that don't have more a's than b's?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to extract specific part of a string in java
    By bhoots21304 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2014, 01:03 PM
  2. extract specific text from a string
    By smemamian in forum Android Development
    Replies: 2
    Last Post: January 18th, 2014, 05:42 AM
  3. Counting Vowels and Consonants in a String.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 8th, 2013, 09:59 PM
  4. Trying to create an alphabet-only string of any length
    By skw4712 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2012, 06:39 AM
  5. Get characters between specific elements in string
    By hacikho in forum Java Theory & Questions
    Replies: 1
    Last Post: November 23rd, 2011, 07:51 PM