From d6eba78971cb024405528caaf766e8cf8ce7dd8a Mon Sep 17 00:00:00 2001 From: Sophia Date: Thu, 5 Sep 2024 19:12:36 +0900 Subject: [PATCH 1/2] valid-palindrome solution --- valid-palindrome/seona926.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 valid-palindrome/seona926.js diff --git a/valid-palindrome/seona926.js b/valid-palindrome/seona926.js new file mode 100644 index 000000000..fbde040af --- /dev/null +++ b/valid-palindrome/seona926.js @@ -0,0 +1,29 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function (s) { + // 1. 아래 방식은 O(n^2)에 수렴함 + // s의 구성요소들을 소문자로 변환 후 배열에 넣고 + // 현재 길이가 2 이상이라면 + // shift === pop 이면 true 아니면 false + + // 2. 투 포인터 사용 + + let str = s.toLowerCase().replace(/[^a-z0-9]/g, ""); + + // 두 포인터 사용: 시작과 끝에서부터 비교 + let left = 0; + let right = str.length - 1; + + while (left < right) { + if (str[left] !== str[right]) { + return false; // 두 문자가 다르면 palindrome 아님 + } + + left++; + right--; + } + + return true; +}; From dd36993c9ef902554d6a75a446eaf3231759245d Mon Sep 17 00:00:00 2001 From: Sophia Date: Thu, 5 Sep 2024 19:15:22 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=8B=9C=EA=B0=84,=20=EA=B3=B5=EA=B0=84?= =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-palindrome/seona926.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/valid-palindrome/seona926.js b/valid-palindrome/seona926.js index fbde040af..e17f36005 100644 --- a/valid-palindrome/seona926.js +++ b/valid-palindrome/seona926.js @@ -27,3 +27,10 @@ var isPalindrome = function (s) { return true; }; + +/* + 1. 시간복잡도: O(n) + - toLowerCase, replace, 포인터 사용에 O(n) 소요됨 + 2. 공간복잡도: O(n) + - 변수 str이 O(n) 차지함 +*/