-
Notifications
You must be signed in to change notification settings - Fork 1
/
125-Valid Palindrome.js
77 lines (68 loc) · 2.02 KB
/
125-Valid Palindrome.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Problem link: https://leetcode.com/problems/valid-palindrome
* 125. Valid Palindrome
* A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters,
* it reads the same forward and backward. Alphanumeric characters include letters and numbers.
* Given a string s, return true if it is a palindrome, or false otherwise.
*
* Solution: Two Pointers
*/
/**
* Slower
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
var alphanum = s.replace(/[\W_]/g, '');
if (!alphanum.length) {
return true;
}
alphanum = alphanum.toLowerCase();
var i = 0, j = alphanum.length - 1;
while (i<=j) {
if (alphanum[i] != alphanum[j]) {
return false;
}
i++;
j--;
}
return true;
};
/**
* @param {string} s
* @return {boolean}
*/
var isPalindromeFaster = function(s) {
let i = 0, j = s.length - 1;
function charOrDigit(i) {
return (
('0' <= s[i] && s[i] <= '9')
|| ('A' <= s[i] && s[i] <= 'Z')
|| ('a' <= s[i] && s[i] <= 'z')
);
}
while (i < j) {
if (!charOrDigit(i)) {
i++;
} else if (!charOrDigit(j)) {
j--;
} else {
if (s[i].toLowerCase() !== s[j].toLowerCase()) {
return false;
}
i++;
j--;
}
}
return true;
};
// another solution to use stack;
/**
* var alphanum = s.replace(/[\W_]/gi, '').toLowerCase().split('');
* 1st loop: stack.push(each alphanum)
* 2nd loop: stack.pop() != alphanum[i] return false [0<=i<alphanum.length]
*/
console.log("result 1: ", isPalindrome("A man, a plan, a canal: Panama")); // true
console.log("result 1: ", isPalindrome(" ")); // true
console.log("result 2: ", isPalindrome( "race a car")); // false
console.log("result 2: ", isPalindrome( "ab_a? #$%^&*()_+")); // true