아스키 코드를 보면,
a
~ z
가 97
~ 122
로 표현됩니다
이를 이용하여 인덱스를 구할 수 있습니다.
97
(a
의 아스키코드)을 뺍니다a => 97 => 97 - 97 => 0 => a 인덱스는 0
b => 98 => 98 - 97 => 1 => b 인덱스는 1
c => 99 => 99 - 97 => 2 => b 인덱스는 2
위와같은 방법으로 인덱스를 구하면 됩니다.
반대로 인덱스를 다시 아스키코드로 바꿀땐 97
(a
의 아스키코드)을 다시 더해주면 됩니다
인덱스
+ 97
(a
의 아스키코드)
function maxOccurenceAlphabet(string) {
const stringList = string.split('');
const aASCIICode = 'a'.charCodeAt(0);
const store = stringList.reduce((acc, cur) => {
const stringToASCII = cur.charCodeAt(0);
if (stringToASCII >= 97 && stringToASCII <= 122) {
const index = stringToASCII - aASCIICode;
acc[index] += 1;
}
return acc;
}, Array(26).fill(0));
let maxOccurence = 0;
let maxAlphabetIndex = 0;
store.forEach((number, index) => {
if (maxOccurence < number) {
maxOccurence = number;
maxAlphabetIndex = index;
}
});
return String.fromCharCode(maxAlphabetIndex + aASCIICode);
}
def find_max_occurred_alphabet(string):
alphabet_occurrence_array = [0] * 26
for char in string:
if not char.isalpha():
continue
arr_index = ord(char) - ord('a')
alphabet_occurrence_array[arr_index] += 1
max_occurrence = 0
max_alphabet_index = 0
for index in range(len(alphabet_occurrence_array)):
alphabet_occurrence = alphabet_occurrence_array[index]
if alphabet_occurrence > max_occurrence:
max_occurrence = alphabet_occurrence
max_alphabet_index = index