Skip to content

Commit c8f9c64

Browse files
Update 0125-valid-palindrome.js
* Clarified some variable and internal function names * Removed if/else conditions * Introduced new variables to increase clarity
1 parent 2202a60 commit c8f9c64

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

javascript/0125-valid-palindrome.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,24 @@ var isPalindrome = function(s) {
6464
* @return {boolean}
6565
*/
6666
var isPalindrome = function (s) {
67-
const isAlpha = c => (c.toLowerCase() >= 'a' && c.toLowerCase() <= 'z') || c >= '0' && c <= '9'
67+
const isAlphaNumeric = c => (c.toLowerCase() >= 'a' && c.toLowerCase() <= 'z') || c >= '0' && c <= '9'
6868

69-
let i = 0;
70-
let j = s.length - 1;
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; }
7176

72-
while (i < j) {
73-
if (!isAlpha(s.charAt(i))) {
74-
i++
75-
} else if (!isAlpha(s.charAt(j))) {
76-
j--
77-
} else if (s.charAt(i).toLowerCase() !== s.charAt(j).toLowerCase()) {
78-
return false
79-
} else {
80-
i++
81-
j--
82-
}
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--
8385
}
8486
return true
8587
};

0 commit comments

Comments
 (0)