카테고리 없음

[LeetCode] 2185. Counting Words With a Given Prefix - 자바스크립트 0614

codevil 2022. 6. 14. 11:26

https://leetcode.com/problems/counting-words-with-a-given-prefix/

 

Counting Words With a Given Prefix - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제를 너무 어렵게 읽어서 어렵게 시도하다 틀리고 다시풀었습니다 ..-_ㅠ

문제는 아주 간단합니다

words가 prefix를 포함하고 있다면 한개씩 숫자를 더하여 return 하는 문제입니다.

1. count = 0 을 선언하고 words를 기준으로 for 루프합니다. 

    let count = 0
    for (let i = 0; i < words.length; i++) {
    
    }

2. word가 pref를 includes하고 있고, word의 맨앞에 위치하고 있으면 count를 더합니다

    for (let i = 0; i < words.length; i++) {
        if (words[i].includes(pref) && words[i].indexOf(pref) === 0) {
            count++
        }
    }

최종풀이

var prefixCount = function (words, pref) {
    let count = 0
    for (let i = 0; i < words.length; i++) {
        if (words[i].includes(pref) && words[i].indexOf(pref) === 0) {
            count++
        }
    }
    return count
};

 

pref와 words가 includes가 아닌 몇 글자가 겹치는지(?) 푸는 문제인줄알구 시간이 생각보다 걸리긴 했지만.. 이내 다시 제대로 읽고 제대로 풀었다.. 문제를 잘읽어야겠다..