[leetcode, JS] 2255. Count Prefixes of a Given String

mxxn·2023년 12월 19일
0

leetcode

목록 보기
151/198

문제

문제 링크 : Count Prefixes of a Given String

풀이

/**
 * @param {string[]} words
 * @param {string} s
 * @return {number}
 */
var countPrefixes = function(words, s) {
    return words.filter(word => s.indexOf(word) === 0).length
};
  1. filter로 words의 element들이 문자열 s에서의 index가 0인 경우를 찾아 length를 return
  • Runtime 57 ms, Memory 42.11 MB

다른 풀이

/**
 * @param {string[]} words
 * @param {string} s
 * @return {number}
 */
var countPrefixes = function(words, s) {
    return words.filter(word => s.startsWith(word)).length
};
  1. filter를 사용하는 것은 같고, startsWith로 찾아 length를 return
  • Runtime 52 ms, Memory 42.14 MB
profile
내일도 글쓰기

0개의 댓글