Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

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);
};
Loading