题解 | #计算某字符出现次数#

计算某字符出现次数

https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
const inputArr = [];//存放输入的数据
rl.on('line', function(line){
  //line是输入的每一行,为字符串格式
    inputArr.push(line);//将输入流保存到inputArr中(注意为字符串数组)
}).on('close', function(){
    console.log(fun(inputArr))//调用函数并输出
})

function fun(inputArr){
    let map = new Map(), key;
    const arrstr = inputArr[0];
    const arrkey = inputArr[1];
    for(let i=0;i<arrstr.length;i++){
        if(arrstr[i] !== ' '){
            key = arrstr[i].toLowerCase();
            let num = Number(map.get(key)) || 0;
            map.set(key, ++num);
        }
    }

    return Number(map.get(arrkey.toLowerCase())) || 0;
}

全部评论

相关推荐

10-17 23:18
已编辑
西北农林科技大学 Web前端
独行m:给25可以试试,但他只能给12,那就是纯纯的事精
秋招,不懂就问
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务