'Are you able to help me understand this Java code?

I am confused about a few things here. The ++alpha/--alpha and the inside of the array [c - 'a']. I ran it through debug and I noticed a 1 gets stored. But I don't understand why this is the case and what the - 'a' is doing.

boolean test = true;
String r = "aa";
String m = "baab";
int[] alpha = new int[26];
        
for(char c: m.toCharArray())
   ++alpha[c - 'a'];
        
for(char c: r.toCharArray()){
   if(alpha[c - 'a'] != 0)
        --alpha[c - 'a'];
   else
        test = false;
}
System.out.print(test);


Solution 1:[1]

The code counts the number of characters by making full use of the fact that a char can be cast to an integer and used in an array int[] alpha = new int[26]; that is 26 characters long to correspond to the letters of the alphabet, and requires minimal code to do so.

In short, the following code simply adds ++ or +1 to the array whenever the corresponding character is found in the m string:

for(char c: m.toCharArray())
   ++alpha[c - 'a'];

And the second loop simply subtracts -- or -1 from the array whenever the corresponding character is found in the r string:

for(char c: r.toCharArray())
   if(alpha[c - 'a'] != 0)
        --alpha[c - 'a'];

In more detail:

The ++ and -- simply increment or decrement the associated variable, and putting it before the variable ++alpha[x] just means it will perform the operation and then assign the incremented value rather than the other way around if it was after alpha[x]++, however, that makes no difference in this code example though:

For example for the following code:

alpha[0] = 0;
++alpha[0]; //or alpha[0]++;
System.out.println("The value was: " + alpha[0]);

We get this result:

The value was: 1

As for [c - 'a'], the value of a char for example 'a' is actually 97 which we can see by doing System.out.println((int)'a');, and this is helpful because it means we can use a char variable directly instead of an integer to write less code in some cases. So it is really just [c - 97], however, if we look at it in context we can see that c is also a character from the m string, so for the first char we get:

String m = "baab";
for(char c: m.toCharArray()){
    ++alpha[c - 'a'];

The c is equal to 'b' in the first iteration of the loop like so:

    ++alpha['b' - 'a'];

But we know that a char behaves like an integer where a=97 and b=98 like so:

    ++alpha[98 - 97];

And for the first char 'b' in the loop it actually ends up resulting in:

++alpha[1];//98-97=1

And if we now expand the ++ it look like this:

alpha[1] = alpha[1] + 1;

If the character was a z for example it would work the same way where ['z'-'a'] is the same as [122-97] which equates to [25]:

alpha['z'-'a'] = alpha['z'-'a'] + 1;

//which is the same as
alpha[122-97] = alpha[122-97] + 1;

//which equates to
alpha[25] = alpha[25] + 1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1