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

计算某字符出现次数

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

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

// 方法一:不使用大小写转换方法
void (async function () {
    const str = await readline();
    const char = await readline();
    let cnt = 0;
    // 97 - 65 = 32
    // 65 + 26 = 81
    for(const c of str){
        if(c === char || Math.abs(c.charCodeAt()- char.charCodeAt()) === 32 && Math.min(c.charCodeAt(), char.charCodeAt())>=65&& Math.min(c.charCodeAt(),char.charCodeAt())<=80) cnt ++
    }
    console.log(cnt);
})();

// 方法二:不使用大小写转换方法
void async function(){
    const str = await readline();
    const char = await readline();
    let cnt = 0;
    for(const c of str){
        if(char.toLowerCase() === c.toLowerCase()) cnt++;
    }
    console.log(cnt);
}()


全部评论

相关推荐

2025-12-17 17:15
华东师范大学 运营
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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