์ฝ๋ฉํ
์คํธ/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
};
