the question is:
Write a method with the signature:
public static void printABs(int n)that prints all the strings, less than or equal to length n, that can be built from the alphabet A,B. printABs must print out its strings
starting from the shortest strings and ending in the longest. So, for
example, the call:
printABs(2)
will print:
A
B
AA
AB
BA
BB

we can use the queue interface method such as empty front remove add.and this question is supposed to use queue.

ive no idea how to start off with this,it kinda permutation thing,but how can a queue do this?need help...!

Recommended Answers

All 3 Replies

Does this help?

public static void printABs(int n)
{
    // Loop: start at i=1 and go to i=n

        // Create a queue, and add "A" and "B"

        // For every character X in the queue, Print out strings that are i characters long, starting with X
}

Just keep creating queues and adding "A" and "B" to them, then use those values when you need them.

A more efficient way would be to use an array, or even just a few for loops. But you need to use a queue, so...

I'm not sure if this is what you meant, Leiger, but the way I'd go about it would be:

- put "A" and "B" in the queue

- pull Strings from the queue: for each String s in the queue, put s+"A", s+"B"
- repeat. Stop when you get to strings longer than n.

Trouble with this is it doesn't keep you in sorted order. you'll get A, AA, AB, B, BA, BB, etc. but it's a start.

Yes, that's what I was trying to say :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.