[LeetCode] 1910. Remove All Occurrences of a Substring


に答える


indexOfを用いて無限ループ中のs内にpartがあるかどうかを解く

コード#コード#

/**
 * @param {string} s
 * @param {string} part
 * @return {string}
 */
var removeOccurrences = function(s, part) {
    // 무한루프
    while(true){
        // part가 있는지 체크
        const index = s.indexOf(part);
        if(index === -1){
            // 없다면 탈출
            break;
        } else {
            // 있다면 빈 문자열로 교체
            s=s.replace(part,'')
        }
    }

    return s;
};

整理する


ソース


https://leetcode.com/problems/remove-all-occurrences-of-a-substring/