From 612a577ba185d4e9b700c265ee11154e8208d624 Mon Sep 17 00:00:00 2001 From: suhwan2004 Date: Thu, 12 Dec 2024 12:33:48 +0900 Subject: [PATCH 1/2] Valid Palindrome --- suhwan2004/Valid Palindrome.js | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 suhwan2004/Valid Palindrome.js diff --git a/suhwan2004/Valid Palindrome.js b/suhwan2004/Valid Palindrome.js new file mode 100644 index 0000000..9b216ef --- /dev/null +++ b/suhwan2004/Valid Palindrome.js @@ -0,0 +1,49 @@ +/* +Time : O(N), Space : O(N) +ALGO : two pointer +DS : string +Constraints + - 1 <= s.length <= 2 * 105 + - s consists only of printable ASCII characters. +Edge case : X +*/ + +var isPalindrome = function (s) { + let start = 0, + end = s.length - 1; + + while (start !== end && start <= end) { + const sChar = changeLowerCase(s[start]); + const eChar = changeLowerCase(s[end]); + + if (!checkValidChar(sChar)) { + start++; + continue; + } else if (!checkValidChar(eChar)) { + end--; + continue; + } + + if (sChar !== eChar) { + return false; + } else { + start++; + end--; + } + } + + return true; +}; + +const changeLowerCase = (c) => { + const charCd = c.charCodeAt(); + if (charCd >= 65 && charCd <= 90) { + return c.toLowerCase(); + } + return c; +}; + +const checkValidChar = (c) => { + const charCd = c.charCodeAt(); + return (charCd >= 97 && charCd <= 122) || (charCd >= 48 && charCd <= 57); +}; From 29e4c4be7bc54a6b08ce2633882452c55b12cf68 Mon Sep 17 00:00:00 2001 From: suhwan2004 Date: Sun, 15 Dec 2024 23:22:47 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=8F=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suhwan2004/Valid Palindrome.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/suhwan2004/Valid Palindrome.js b/suhwan2004/Valid Palindrome.js index 9b216ef..7c5b08c 100644 --- a/suhwan2004/Valid Palindrome.js +++ b/suhwan2004/Valid Palindrome.js @@ -26,10 +26,10 @@ var isPalindrome = function (s) { if (sChar !== eChar) { return false; - } else { - start++; - end--; } + + start++; + end--; } return true;