题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout }); const inputs = []; rl.on('line', (line) => { inputs.push(line.trim()); }).on('close', () => { // 不区分大小写,那就全部转成小写再匹配 let str = inputs[0].toLowerCase(); const key = inputs[1].toLowerCase(); let times = 0; for(let i = 0; i < str.length; i++) { if(str[i] === key) { times += 1; } } console.log(times); })
这道题仅仅查找单个字符 视情况而定用哈希吧,就单解题的情景下没必要