์ฝ”๋”ฉํ…Œ์ŠคํŠธ/LeetCode

[LeetCode] search-suggestions-system

์œค์ฝ”๋”ฉ 2022. 6. 19. 16:09

๋ฌธ์ œ๋งํฌ

 

Search Suggestions System - 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

 

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด products์˜ ์š”์†Œ ์ค‘ ์ฃผ์–ด์ง„ ๊ฒ€์ƒ‰ ๋‹จ์–ด searchWord์˜ ์ฒซ ๋‹จ์–ด, ์ฒซ ๋‹จ์–ด + ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด, ์ฒซ ๋‹จ์–ด + ๋‘๋ฒˆ์งธ ๋‹จ์–ด + ์„ธ๋ฒˆ์งธ ๋‹จ์–ด... ๋ชจ๋“  ๋‹จ์–ด ์™€ ์ผ์น˜ํ•˜๋Š” ๋‹จ์–ด๋“ค์„ ์ฐพ์•„ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์•ž 3๋‹จ์–ด๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
(ex. products = ['a', 'abc', 'abd, 'eed', 'abcd'], searchWord = "abcde" ์ผ๋•Œ
output์€ [['a', 'abc', 'abcd'], ['abc', 'abcd', 'abd'], ['abc', 'abcd'], ['abcd'], []] ์ด ์ถœ๋ ฅ๋œ๋‹ค.
(output[0]์—์„  ['a', 'abc', 'abcd', 'abd']๊ฐ€ ๊ฒ€์ƒ‰๋˜์ง€๋งŒ 3๊ฐœ๋ฅผ ์ž˜๋ผ์„œ 'abd'๊ฐ€ ์ž˜๋ฆฐ ๊ฒƒ์ด๋‹ค.))

์ ‘๊ทผ ๋ฐฉ๋ฒ•

๋จผ์ € output์€ ๋ฌด์กฐ๊ฑด searchWord์˜ ๊ธธ์ด๋งŒํผ 2์ฐจ์› ๋ฐฐ์—ด์ด ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ฃผ์–ด์ง„ searchWord์˜ ๋‹จ์–ด๋ฅผ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•˜๋ฉฐ ๊ฒ€์ƒ‰ํ•ด์•ผํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผํ•˜์˜€๋‹ค.
for(let i = 0; i < searchWord.length; i++){
    let regWord = new RegExp(searchWord.slice(0,i+1))
    console.log(regWord)
}
/*
์ถœ๋ ฅ
/a/
/ab/
/abc/
/abcd/
/abcde/
*/โ€‹


๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  products์˜ ์š”์†Œ๋“ค์„ filter ํ•˜๋ฉฐ ์ •๊ทœ์‹์œผ๋กœ ๋งŒ๋“  regWord์™€ match๋ ๋•Œ๋งŒ output ๋ฐฐ์—ด์— push ํ•˜์˜€๋‹ค.


๋ฐฐ์šด ์ 

1.
// ์ฒ˜์Œ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ๋ฐฉ๋ฒ•
let output = new Array(searchWord.length).fill([])

// ๊ณ ์นœ ๋ฐฉ๋ฒ•
let output = Array.from({length : searchWord.length}, () => [])โ€‹

 

์ฒ˜์Œ ๋ฐฉ๋ฒ•๋Œ€๋กœ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋ฉด fill([])์˜ ๋ฐฐ์—ด๋“ค์ด ๋ชจ๋‘ ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค.
๋•Œ๋ฌธ์— output[0]์— ์ž„์˜์˜ ๊ฐ’์„ pushํ•˜๋ฉด output์˜ ๋ชจ๋“  []์— ๊ฐ’์ด ๊ฐ™์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

๋•Œ๋ฌธ์— Array.from ๋ฐฉ์‹์œผ๋กœ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์˜€๋‹ค.

 
2.
// ์ฒ˜์Œ ์ •๊ทœ์‹์œผ๋กœ filterํ•œ ๋ฐฉ๋ฒ•
products.filter(product => {
    if(product.match(regWord)){
        output[i].push(product)
    }
})

// ๊ณ ์นœ ๋ฐฉ๋ฒ•
products.filter(product => {
    if(product.match(regWord)){
        if(product.match(regWord).index === 0) output[i].push(product)
    }
})โ€‹

๋ฌธ์ œ์˜ ์กฐ๊ฑด์€ ์ฒ˜์Œ ๋‹จ์–ด๋ถ€ํ„ฐ ๊ฐ™์€ ๋‹จ์–ด๋งŒ ์š”๊ตฌํ•œ๋‹ค. (product : "mouse", searchWord : "mo" ์ผ ๊ฒฝ์šฐ๋งŒ true)
ํ•˜์ง€๋งŒ ์ฒ˜์Œ ๋ฐฉ์‹์€ (product : "toomos", searchWord : "mo" ์ผ ๊ฒฝ์šฐ์—๋„ true์ด๋ฏ€๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.)

๊ทธ๋ž˜์„œ match ๊ฐ’์— index ๋ฒˆํ˜ธ๊ฐ€ 0์ผ ๊ฒฝ์šฐ๋งŒ pushํ•˜๋„๋ก ๋ฐ”๊พธ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ๋Š” ๋„ˆ๋ฌด ์†Œ์š” ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค.

9.5 %..

์•ฝ๊ฐ„ ๋น„ํšจ์œจ์ ์ด๋ผ ์ƒ๊ฐํ•œ ๋ถ€๋ถ„์„ ๊ณ ์ณค๋‹ค.
1. sort()
2. slice()

// ๋งค๋ฒˆ sort()๋ฅผ ํ•˜๋Š”๊ฒŒ ๋น„ํšจ์œจ์ ์ด๋ผ ์ƒ๊ฐํ•˜์—ฌ ๋ฏธ๋ฆฌ ํ•œ๋ฒˆ ์ฒ˜๋ฆฌํ•˜๋‹ˆ ์†๋„๊ฐ€ ์˜ฌ๋ž๋‹ค.
products.sort();

// ๋ชจ๋‘ ๊ตฌํ•ด์„œ 3๊ฐœ๋ฅผ ์ž๋ฅด๋Š” ๊ฒƒ๋„ ๋น„ํšจ์œจ์ ์ด๋ผ ์ƒ๊ฐ๋˜์–ด
output[i] = output[i].slice(0,3)

// push ์กฐ๊ฑด์— output[i].length < 3์ผ๋•Œ๋งŒ push ํ•˜๋„๋ก ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์†๋„๋ฅผ ์˜ฌ๋ ธ๋‹ค.
if(product.match(regWord).index === 0 && output[i].length < 3) {
	output[i].push(product)
    }

์•„์ง๋„ ๋‚ฎ๋‹ค.. ์ ‘๊ทผ ๋ฐฉํ–ฅ์ด ์ž˜๋ชป๋œ๋“ฏํ•˜๋‹ค.

 

// ์ˆ˜์ •ํ•œ ์ฝ”๋“œ
var suggestedProducts = function(products, searchWord) {
    products.sort();
    let output = Array.from({length : searchWord.length}, () => [])
    
    for(let i = 0; i < searchWord.length; i++){
        let regWord = new RegExp(searchWord.slice(0,i+1))
        
        products.filter(product => {
            if(product.match(regWord)){
                if(product.match(regWord).index === 0 && output[i].length < 3) output[i].push(product)
            }
        })
    }
    
    return output
};
// ์ˆ˜์ • ์ „ ์ฝ”๋“œ

/**
 * @param {string[]} products
 * @param {string} searchWord
 * @return {string[][]}
 */
var suggestedProducts = function(products, searchWord) {
    let output = Array.from({length : searchWord.length}, () => [])
    
    for(let i = 0; i < searchWord.length; i++){
        let regWord = new RegExp(searchWord.slice(0,i+1))
        
        products.filter(product => {
            if(product.match(regWord)){
                if(product.match(regWord).index === 0) output[i].push(product)
            }
        })
        
        output[i] = output[i].sort().slice(0,3)
    }
    
    return output
};