λ¬Έμ
νμ΄ κ³Όμ
μ£Όμ΄μ§ λ¬Έμμ΄μμ 10μ μ΄μμ λΆλΆ λ¬Έμμ΄ μ€ 2λ² μ΄μ λ°λ³΅λλ λͺ¨λ λΆλΆ λ¬Έμμ΄μ μ’
λ₯λ₯Ό μ°Ύλ λ¬Έμ μ
λλ€. ν΄μ ν
μ΄λΈ
λ₯Ό μ¬μ©νλ©΄ O(N)
μ λͺ¨λ κ²½μ°λ₯Ό νμν μ μμ΅λλ€.
μ΄μΈμλ λ κ°μ Set
μ μ¬μ©νλ©΄ λμΌν λ‘μ§μ μνν μλ μμ΅λλ€. (ꡬνμ μ΄κ² λ κΉλν κ² κ°μ΅λλ€ π)
μ½λ
/**
* @param {string} s
* @return {string[]}
*/
var findRepeatedDnaSequences = function (s) {
if (s.length < 10) return [];
const cache = new Map();
for (let i = 0, len = s.length; i <= len - 10; i++) {
const substring = s.slice(i, i + 10);
if (cache.has(substring)) {
const quantity = cache.get(substring);
cache.set(substring, quantity + 1);
} else {
cache.set(substring, 0);
}
}
const answer = [...cache.entries()].reduce((acc, [key, value]) => {
if (value >= 1) acc.push(key);
return acc;
}, []);
return answer;
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 236 - Lowest Common Ancestor of a Binary Tree (Medium) (0) | 2021.03.04 |
---|---|
LeetCode 494 - Target Sum (Medium) (0) | 2021.03.04 |
LeetCode 3 - Longest Substring Without Repeating Characters (Medium) (0) | 2021.03.03 |
LeetCode 89 - Gray Code (Medium) (0) | 2021.03.03 |
LeetCode 118 - Pascal's Triangle (Easy) (0) | 2021.03.03 |
π¬ λκΈ