题解 | 穷哈哈~

穷哈哈~

https://www.nowcoder.com/practice/5b3184b233f34fb39a7f259ae82eb42c

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

void (async function () {
    // Write your code here
    let length = await readline();
    let str = await readline();
    const arr = str.split("");
    let l = 0;
    const strTemp = [];
    for (i = 0; i < length; i++) {
        const char = arr[i];
        if (strTemp.length === 0) {
            if (char === "a" || char === "h") {
                strTemp.push(char);
            }
        } else if (strTemp[strTemp.length - 1] === "a") {
            if (char === "h") {
                strTemp.push(char);
            } else {
                l = Math.max(strTemp.length, l);
                strTemp.length = 0;
                if (char === "a" || char === "h") {
                    strTemp.push(char);
                }
            }
        } else if (strTemp[strTemp.length - 1] === "h") {
            if (char === "a") {
                strTemp.push(char);
            } else {
                l = Math.max(strTemp.length, l);
                strTemp.length = 0;
                if (char === "a" || char === "h") {
                    strTemp.push(char);
                }
            }
        }
    }
    l = Math.max(strTemp.length, l);
    console.log(l);
})();

全部评论

相关推荐

点赞 评论 收藏
分享
驼瑞驰_招募评论官版...:一共经历几次握手?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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