From 8d30b5f8592e03219d818ca45a08c9ce0829a520 Mon Sep 17 00:00:00 2001 From: Cyjin-jani Date: Tue, 17 Mar 2026 15:52:14 +0900 Subject: [PATCH 1/2] add: validPalindrome solution --- valid-palindrome/Cyjin-jani.js | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 valid-palindrome/Cyjin-jani.js diff --git a/valid-palindrome/Cyjin-jani.js b/valid-palindrome/Cyjin-jani.js new file mode 100644 index 000000000..c5052d316 --- /dev/null +++ b/valid-palindrome/Cyjin-jani.js @@ -0,0 +1,41 @@ +// naive한 풀이 +// tc: O(n) +// sc: O(n) +const isPalindromeNaive = function (s) { + const str = s.toLowerCase().replace(/[^a-z0-9]/g, ''); // 이 부분에서 공간복잡도가 O(n) + let leftIdx = 0; + let rightIdx = str.length - 1; + + while (leftIdx <= rightIdx) { + if (str[leftIdx] !== str[rightIdx]) { + return false; + } else { + leftIdx++; + rightIdx--; + } + } + return true; +}; + +// 좀 더 최적화 된 풀이 +// tc: O(n) +// cs: O(1) +const isPalindrome = function (s) { + let leftIdx = 0; + let rightIdx = s.length - 1; + + while (leftIdx < rightIdx) { + while (leftIdx < rightIdx && !isAlphaNumeric(s[leftIdx])) leftIdx++; + while (leftIdx < rightIdx && !isAlphaNumeric(s[rightIdx])) rightIdx--; + + if (s[leftIdx].toLowerCase() !== s[rightIdx].toLowerCase()) return false; + + leftIdx++; + rightIdx--; + } + return true; +}; + +function isAlphaNumeric(char) { + return /[a-zA-Z0-9]/.test(char); +} From f886f0b1018ae409a0536abc00bce67fbaf49105 Mon Sep 17 00:00:00 2001 From: Cyjin-jani Date: Tue, 17 Mar 2026 16:14:24 +0900 Subject: [PATCH 2/2] add: numberOf1Bits solution --- number-of-1-bits/Cyjin-jani.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 number-of-1-bits/Cyjin-jani.js diff --git a/number-of-1-bits/Cyjin-jani.js b/number-of-1-bits/Cyjin-jani.js new file mode 100644 index 000000000..e8011574b --- /dev/null +++ b/number-of-1-bits/Cyjin-jani.js @@ -0,0 +1,29 @@ +// tc: O(logn) +// sc: O(logn) +const hammingWeight_simple = function (n) { + const binaryStr = n.toString(2); + let answer = 0; + + for (char of binaryStr) { + if (+char === 1) { + answer++; + } + } + + return answer; +}; + +// toString(2)를 쓰지 않고 처리 +// tc: O(logn) +// sc: O(1) +const hammingWeight = function (n) { + let answer = 0; + + while (n >= 0) { + const remains = n % 2; + if (remains === 1) answer++; + n = Math.floor(n / 2); + } + + return answer; +};