Home > Mobile >  Java Letter combination program for aaa to zzz
Java Letter combination program for aaa to zzz

Time:06-03

I have written this code for a program to create a combination of the the letters aaa to zzz. There are 17,576 combinations. For example first is aaa then aab and so on.

I want my code to count the different combinations such as 1. aaa 2. aab 3. aac and so on during the output.

Here is my code:

for(char c1 = 'a'; c1 <= 'z'; c1  ){
        for(char c2 = 'a'; c2 <= 'z'; c2  ){
            for(char c3 = 'a'; c3 <= 'z'; c3  ){
                System.out.println(""   c1   c2   c3);
                }
            } 
        }

Thank you!

CodePudding user response:

Well you could maintain a counter variable which gets incremented at each execution of the inner loop:

int counter = 0;
List<String> combinations = new ArrayList<>();

for (char c1 = 'a'; c1 <= 'z'; c1  ) {
    for (char c2 = 'a'; c2 <= 'z'; c2  ) {
        for (char c3 = 'a'; c3 <= 'z'; c3  ) {
            String combo = ""   c1   c2   c3;
            System.out.println(combo);
            combinations.add(combo);
              counter;
        }
    } 
}

System.out.println("total number of combinations is "   counter);  // 17576

CodePudding user response:

Here is an alternative implementation.

IntStream of code points

I recommend making a habit of using code points integer numbers rather than the legacy type char when working with individual characters. As a 16-bit value, a char is physically incapable of representing most characters.

We can generate the range of code points from a to z (97 to 122) from an IntStream. The Character.toString( codePoint ) method generates a single-character String object from our code point integer.

List < String > characters =
        IntStream
                .range( "a".codePointAt( 0 ) , "z".codePointAt( 0 )   1 )  // ( 97 inclusive, 123 exclusive )
                .mapToObj( Character :: toString )
                .toList();

characters.toString() = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]

Collect each String of three combined characters into List.

List < String > combinations = new ArrayList <>( characters.size() ^ 3 );

Then use the for-each syntax to loop that source list three times, nested, once for each position of your desired outputs.

for ( String firstCharacter : characters )
{
    for ( String secondCharacter : characters )
    {
        for ( String thirdCharacter : characters )
        {
            combinations.add( firstCharacter   secondCharacter   thirdCharacter );
        }
    }
}

Calling List#size gives you count you desire. Though mathematically we know that count should be ( 26 ^ 3 ) = 17,576.

System.out.println( combinations.size()   " combinations = "   combinations );

When run.

17576 combinations = [aaa, aab, aac, aad, aae, aaf, … zzw, zzx, zzy, zzz]

CodePudding user response:

int n = 26; //no. of alphabet which is fix number
int p = 3;  //no. of alphabet we require in one combination like aaa,abc,bbb....
System.out.print(Math.pow(n,p));

for explanation of the formula read below description which will helpful

      **Easy mathematic formula**
    
    ***(fix number)^(no. of element in each combination)***
    
    It is just like no. of total combination of  when we toss a coin , when we throw two dice in probability.
    
    here each combination contains 3 alphabets so , let's take each alphabet from different pool(p1 , p2 , p3).
    
    p1  p2 p3
    __ ___ __
    a   a  a
    b   b  b
    c   c  c
    d   d  d
    .   .  .
    .   .  .
    .   .  .
    
    so here first take 1st alphabet 'a' from p1(pool1) and then take each alphabet from p2(pool2) so our result be like **aa,ab,ac,ad.....** . here we can see that when we take only 'a' then it creates 26 different combination. likewise each character of pool1(p1) has its own 26 different combination (like 'b' has 'ba','bb','bc'...  also c has'ca','cb','cc' and so on). 
    
    so total combination we can make from first two pool is 26*26.
    
    now let see how our new pool (new combinated pool) look likes...
    
    new pool**    **p3**
_____________     ________
    aa               a
    ab               b
    ac               c
    ad               d
    .                .
    .                .
    .                z
    .               ====
    zz              26
    =====
    26*26 
    
    now let's do the same process between **new pool** and **p3** , so our final result is (new pool characters)*(p3's characters)=[26**26]*26=17576
  • Related