[leetcode, JS] 2062. Count Vowel Substrings of a String

mxxn·2023년 12월 11일
0

leetcode

목록 보기
142/198

문제

문제 링크 : 2062. Count Vowel Substrings of a String

풀이

/**
 * @param {string} word
 * @return {number}
 */
var countVowelSubstrings = function(word) {
    const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
    const set = new Set();
    let count = 0;

    for (let i = 0; i < word.length; i += 1) {
        set.clear();

        for (let j = 0; j + i < word.length && vowels.has(word[j + i]); j += 1) {
        set.add(word[i + j]);

        if (set.size === 5) {
            count += 1;
        }
        }
    }

    return count;
};
  1. 모음 Set을 만들고
  2. 문자열 word를 쪼개서 넣을 Set도 만든 후
  3. for문을 통해 0번째부터 모음이 나올때까지 문자열을 만들어 Set에 add하고 그 size가 5일때만 count++ (모음만 남는 경우)
  • Runtime 59 ms, Memory 43.60 MB
profile
내일도 글쓰기

0개의 댓글