Skip to content

Commit d11684b

Browse files
authored
Merge pull request #2535 from acidbluebriggs/patch-1
2 parents 68f9343 + c8f9c64 commit d11684b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

javascript/0125-valid-palindrome.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,33 @@ var isPalindrome = function(s) {
5555
}
5656
return true;
5757
};
58+
59+
/**
60+
* 2 Pointer | Midde Convergence | No RegEx | No Copying
61+
* Time O(N) | Space O(1)
62+
* https://leetcode.com/problems/valid-palindrome/
63+
* @param {string} s
64+
* @return {boolean}
65+
*/
66+
var isPalindrome = function (s) {
67+
const isAlphaNumeric = c => (c.toLowerCase() >= 'a' && c.toLowerCase() <= 'z') || c >= '0' && c <= '9'
68+
69+
let left = 0;
70+
let right = s.length - 1;
71+
let skipLeft, skipRight, endsEqual = false;
72+
73+
while (left < right) {
74+
skipLeft = !isAlphaNumeric(s.charAt(left))
75+
if (skipLeft) { left++; continue; }
76+
77+
skipRight = !isAlphaNumeric(s.charAt(right))
78+
if (skipRight) { right--; continue; }
79+
80+
endsEqual = s.charAt(left).toLowerCase() === s.charAt(right).toLowerCase()
81+
if (!endsEqual) return false
82+
83+
left++
84+
right--
85+
}
86+
return true
87+
};

0 commit comments

Comments
 (0)