Skip to content

Commit

Permalink
Valid Palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
suhwan2004 committed Dec 12, 2024
1 parent c2491e0 commit 612a577
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions suhwan2004/Valid Palindrome.js
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit 612a577

Please sign in to comment.