diff --git a/climbing-stairs/HerrineKim.js b/climbing-stairs/HerrineKim.js new file mode 100644 index 000000000..37dcbf3e1 --- /dev/null +++ b/climbing-stairs/HerrineKim.js @@ -0,0 +1,18 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function (n) { + if (n <= 2) return n; + let prev1 = 1, prev2 = 2; + for (let i = 3; i <= n; i++) { + let curr = prev1 + prev2; + prev1 = prev2; + prev2 = curr; + } + return prev2; +}; + diff --git a/decode-ways/HerrineKim.js b/decode-ways/HerrineKim.js new file mode 100644 index 000000000..ea2335cd2 --- /dev/null +++ b/decode-ways/HerrineKim.js @@ -0,0 +1,27 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +/** + * @param {string} s + * @return {number} + */ +var numDecodings = function (s) { + const memo = {}; + + const helper = (index) => { + if (index === s.length) return 1; + if (s[index] === '0') return 0; + if (memo[index] !== undefined) return memo[index]; + + let ways = helper(index + 1); + if (index < s.length - 1 && parseInt(s.slice(index, index + 2)) <= 26) { + ways += helper(index + 2); + } + + memo[index] = ways; + return ways; + }; + + return helper(0); +}; + diff --git a/valid-anagram/HerrineKim.js b/valid-anagram/HerrineKim.js new file mode 100644 index 000000000..a10713246 --- /dev/null +++ b/valid-anagram/HerrineKim.js @@ -0,0 +1,27 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + if (s.length !== t.length) return false; + + const charCount = {}; + + for (const char of s) { + charCount[char] = (charCount[char] || 0) + 1; + } + + for (const char of t) { + if (!charCount[char]) { + return false; + } + charCount[char]--; + } + + return true; +}; +