题解 | #计算某字母出现次数#
计算某字母出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
用一个数组存放即可,数组 + 正则
let arr = [];
while(line = readline()) {
arr.push(line)
}
let s = arr[0];
let index = arr[1];
const letterCount = new Array(26).fill(0);
let len = s.length;
for (let i = 0; i < len; i += 1) {
if (/[A-Z]/.test(s[i])) {
letterCount[s.charCodeAt(i) - 'A'.charCodeAt(0)] += 1;
}
if (/[a-z]/.test(s[i])) {
letterCount[s.charCodeAt(i) - 'a'.charCodeAt(0)] += 1;
}
}
console.log(/[a-z]/.test(index) ? letterCount[index.charCodeAt(0) - 'a'.charCodeAt(0)] : letterCount[index.charCodeAt(0) - 'A'.charCodeAt(0)])