# A fun algorithm for All possible combinations of alphabets

posted 5 years ago

Hi

yesterday I saw this Post about All possible combinations of alphabets.

Problem was to generate all combination of 3 and 4 characters input e.g. abc or abcd.

I know this problem is not very difficult and can be solved without much brain storming but I have come up with a rather different algorithm for this.

If you see "aaaa" or "aaa" first thought comes up that they are Strings but if you think in terms of "base 36" number system these normal strings can be converted into integers.

So if you want every combination of "aaaa" to "zzzz" then you just have to iterate from integer aaaa to zzzz.

so in base 36:

aaaa+1 == aaab similarly upto aaaz

....

but after aaaz you have to add 11

aaaz+11 == aaba

similarly if you take care of appropriate increments then you can generate all strings in one go:

this is the code for printing all combinations for 4 letter strings:

I also wrote a general recursive function using same approach for generating all combinations of Strings of any number of characters.

I am not saying that this algo is very efficient, and of course this algo will be of limited use because of the limitations of int size.

What do you think?

yesterday I saw this Post about All possible combinations of alphabets.

Problem was to generate all combination of 3 and 4 characters input e.g. abc or abcd.

I know this problem is not very difficult and can be solved without much brain storming but I have come up with a rather different algorithm for this.

If you see "aaaa" or "aaa" first thought comes up that they are Strings but if you think in terms of "base 36" number system these normal strings can be converted into integers.

So if you want every combination of "aaaa" to "zzzz" then you just have to iterate from integer aaaa to zzzz.

so in base 36:

aaaa+1 == aaab similarly upto aaaz

....

but after aaaz you have to add 11

aaaz+11 == aaba

similarly if you take care of appropriate increments then you can generate all strings in one go:

this is the code for printing all combinations for 4 letter strings:

I also wrote a general recursive function using same approach for generating all combinations of Strings of any number of characters.

I am not saying that this algo is very efficient, and of course this algo will be of limited use because of the limitations of int size.

**but its just a different and fun approach.**What do you think?

Piyush

posted 5 years ago

I've done something like this when trying to count how many times you need each 'digit' to write the numbers 1 - 999999. I realized that it is very similar to a car's odometer, and proceeded from there.

Of course, the downside is that it doesn't find the string "ab" for the input "abcd", but that may not be part of your requirements.

Of course, the downside is that it doesn't find the string "ab" for the input "abcd", but that may not be part of your requirements.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |