Skip to main content

2185.js

/** ------------------------------------------------------------------------------
*
* 2185. Counting Words With a Given Prefix
* Topics: Array, String
* https://leetcode.com/problems/counting-words-with-a-given-prefix/?envType=daily-question&envId=2025-01-09
*
------------------------------------------------------------------------------ */
/**
* @param {string[]} words
* @param {string} pref
* @return {number}
*/
var prefixCount = function (words, pref) {
return words.filter((w) => w.startsWith(pref)).length
}

/**
* 5초풀이에서 공간복잡도 줄이기
*/
var prefixCount = function (words, pref) {
let answer = 0
for (let word of words) {
if (word.startsWith(pref)) {
answer++
}
}
return answer
}

/**
* 가장 최적화
*/
var prefixCount = function (words, pref) {
let count = 0
for (let i = 0; i < words.length; i++) {
if (words[i].indexOf(pref) === 0) count++
}
return count
}

console.log(prefixCount(["pay", "attention", "practice", "attend"], "at"))