From ac0d67a8248b83945fb20e15acc159bbfe91bca5 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Tue, 20 Aug 2024 12:29:08 +0900 Subject: [PATCH 1/5] Valid Anagram --- valid-anagram/hyejjun.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 valid-anagram/hyejjun.js diff --git a/valid-anagram/hyejjun.js b/valid-anagram/hyejjun.js new file mode 100644 index 000000000..3085fbd31 --- /dev/null +++ b/valid-anagram/hyejjun.js @@ -0,0 +1,28 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + if (s.length !== t.length) return false; + + let countS = {}; + let countT = {}; + + for (let i = 0; i < s.length; i++) { + countS[s[i]] = (countS[s[i]] || 0) + 1; + countT[t[i]] = (countT[t[i]] || 0) + 1; + } + + for (let key in countS) { + if (countS[key] !== countT[key]) { + return false; + } + } + + return true; + +}; + +console.log(isAnagram("anagram", "nagaram")); +console.log(isAnagram("rat", "car")); From 205eb5d36045d65a956d34042d3646adab916530 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Tue, 20 Aug 2024 17:45:20 +0900 Subject: [PATCH 2/5] Counting Bits --- counting-bits/hyejjun.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 counting-bits/hyejjun.js diff --git a/counting-bits/hyejjun.js b/counting-bits/hyejjun.js new file mode 100644 index 000000000..fc6d0f201 --- /dev/null +++ b/counting-bits/hyejjun.js @@ -0,0 +1,26 @@ +/** + * @param {number} n + * @return {number[]} + */ +var countBits = function (n) { + + let result = []; + + for (let i = 0; i <= n; i++) { + + let binary = i.toString(2); + let sum = 0; + + for (let char of binary) { + sum += Number(char); + } + result.push(sum); + } + + return result; + +}; + + +console.log(countBits(2)); +console.log(countBits(5)); \ No newline at end of file From 59901d1cdf5c76dfa3c7d31b491feef535d86d22 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Tue, 20 Aug 2024 17:58:44 +0900 Subject: [PATCH 3/5] =?UTF-8?q?line=20break=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- counting-bits/hyejjun.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/counting-bits/hyejjun.js b/counting-bits/hyejjun.js index fc6d0f201..d6a4f9d43 100644 --- a/counting-bits/hyejjun.js +++ b/counting-bits/hyejjun.js @@ -23,4 +23,4 @@ var countBits = function (n) { console.log(countBits(2)); -console.log(countBits(5)); \ No newline at end of file +console.log(countBits(5)); From 8a5c984eb53961fad6eeb297f52a78de213d4119 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Fri, 23 Aug 2024 17:21:14 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EC=8B=9C=EA=B3=B5=EA=B0=84=20=EB=B3=B5?= =?UTF-8?q?=EC=9E=A1=EB=8F=84=20=EC=A3=BC=EC=84=9D=EC=97=90=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- counting-bits/hyejjun.js | 5 +++++ valid-anagram/hyejjun.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/counting-bits/hyejjun.js b/counting-bits/hyejjun.js index d6a4f9d43..48f97d9c4 100644 --- a/counting-bits/hyejjun.js +++ b/counting-bits/hyejjun.js @@ -24,3 +24,8 @@ var countBits = function (n) { console.log(countBits(2)); console.log(countBits(5)); + +/* +시간 복잡도: O(n * log n) +공간 복잡도: O(n) +*/ diff --git a/valid-anagram/hyejjun.js b/valid-anagram/hyejjun.js index 3085fbd31..1888ce81e 100644 --- a/valid-anagram/hyejjun.js +++ b/valid-anagram/hyejjun.js @@ -26,3 +26,8 @@ var isAnagram = function (s, t) { console.log(isAnagram("anagram", "nagaram")); console.log(isAnagram("rat", "car")); + +/* +시간 복잡도: O(n) +공간 복잡도: O(n) +*/ From c67f669e0516fe903bc9a3d808d9566aaad876a4 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Fri, 23 Aug 2024 22:07:00 +0900 Subject: [PATCH 5/5] Num Decodings --- decode-ways/hyejjun.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 decode-ways/hyejjun.js diff --git a/decode-ways/hyejjun.js b/decode-ways/hyejjun.js new file mode 100644 index 000000000..83b4d5f0d --- /dev/null +++ b/decode-ways/hyejjun.js @@ -0,0 +1,42 @@ +/** + * @param {string} s + * @return {number} + */ + +var numDecodings = function (s) { + if (s == null || s.length === 0) { + return 0; + } + + const n = s.length; + const dp = new Array(n + 1).fill(0); + dp[0] = 1; + dp[1] = s[0] === '0' ? 0 : 1; + + for (let i = 2; i <= n; i++) { + const oneDigit = parseInt(s.substring(i - 1, i)); + const twoDigits = parseInt(s.substring(i - 2, i)); + + // Check if single digit decoding is possible + if (oneDigit >= 1 && oneDigit <= 9) { + dp[i] += dp[i - 1]; + } + + // Check if two digit decoding is possible + if (twoDigits >= 10 && twoDigits <= 26) { + dp[i] += dp[i - 2]; + } + } + + return dp[n]; +}; + + +console.log(numDecodings("12")); +console.log(numDecodings("226")); +console.log(numDecodings("06")); + +/* +시간 복잡도: O(n) +공간 복잡도: O(n) +*/