In "Cracking the Coding Interview" there is a question that wants you to check if two strings are a permutation of one another. After looking at the code, I am confused about why the author's example implementation indexes an array using "char" value of the first string and then after that gets the char values of the second string but casts it into an int before then accessing the same array. Here is a snippet of the code below. You can see that in the first for loop it uses a char value but in the second for loop it casts the char to an int before accessing.:
boolean isPermutation(String str1, String str2) {
if (str1.length() != str2.length()) {
return false;
}
int[] charCount = new int[128];
char[] s_array = str1.toCharArray();
for (char c : s_array) {
charCount[c]++;
}
for (int i = 0; i < str2.length(); i++) {
int c = (int) str2.charAt(i);
charCount[c]--;
if (charCount[c] < 0) {
return false;
}
}
return true;
}