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

计算某字符出现次数

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

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const getAppearNum = (str: string, target: string) => {
    const map = {};
    for (let i = 0; i < str.length; i++) {
        const item = str.charAt(i).toLocaleUpperCase();
        if (!map[item]) {
            map[item] = 1;
        } else {
            map[item] += 1;
        }
    };
    return map[target.toLocaleUpperCase()] || 0;
};

let i = 0;
let inputStr = "";
let target = "";
rl.on("line", function (line) {
    i++;
    if (i === 1) {
        inputStr = line;
    } else {
        target = line;
        console.log(getAppearNum(inputStr, target));
    }
});

全部评论

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务