[leetcode, JS] 1408. String Matching in an Array

mxxn·2023년 10월 16일
0

leetcode

목록 보기
96/198

문제

문제 링크 : String Matching in an Array

풀이

/**
 * @param {string[]} words
 * @return {string[]}
 */
var stringMatching = function(words) {
    const result = []
    words.forEach((e, i, origin) => {
        let tempArr = [...origin]
        tempArr.splice(i, 1)
        if(tempArr.filter(el => el.includes(e)).length > 0) {
            result.push(e)
        }
        
    })
    return result
};
  1. 배열 words를 forEach로 순회하며 각 element가 다른 문자열게 속하는지 확인
  • Runtime 47 ms, Memory 43.3 MB (메모리 비효율적)

다른 풀이

/**
 * @param {string[]} words
 * @return {string[]}
 */
var stringMatching = function(words) {
    var arr = [];
    for(i=0;i<words.length;i++){
        for(j=0;j<words.length;j++){
            if(words[i] !== words[j] && words[j].includes(words[i])){
                arr.push(words[i]);
            }
        }
    }
    return [...new Set(arr)];
};
  1. 같은 풀이 다른 방식
  • Runtime 55 ms, Memory 41.8 MB
profile
내일도 글쓰기

0개의 댓글