Get alphabetical of each letter of word

  1. User ASCII to get the index of every word
1
2
3
4
5
6
7
8
9
10
class Solution {
public int[] getOrder(String word) {
// only lowercase letters
int[] letter_array = new int[26];
for(int i=0;i<word.length();i++) {
letter_array[word.charAt(i) - 'a']++;
}
return letter_array;
}
}
1
2
3
4
5
6
7
8
9
10
class Solution {
// include uppercase letters
public int[] getOrder(String word) {
int[] letter_array = new int[60];
for(int i=0;i<word.length();i++) {
letter_array[word.charAt(i) - 'A']++;
}
return letter_array;
}
}