-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.js
46 lines (35 loc) · 938 Bytes
/
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
const checkPalindrome = (str) => {
let L = 0;
let R = str.length - 1;
while (L <= R) {
if (str[L] !== str[R]) return false;
L++;
R--;
}
return true;
}
// console.log(checkPalindrome("bbbc"));
// LC 125: consider only alphanumeric
const isPalindrome = (s) => {
let modifiedString = ""
for (const letter of s) {
if (letter === " ") continue;
if (myIncludes(letter.toLowerCase())) modifiedString += letter.toLowerCase();
console.log(modifiedString);
}
let L = 0;
let R = modifiedString.length - 1;
while (L <= R) {
if (modifiedString[L] !== modifiedString[R]) return false;
L++;
R--;
}
return true;
};
const myIncludes = (letter) => {
const alpha = 'abcdefghijklmnopqrstuvwxyz0123456789'
if (alpha.includes(letter)) return true;
return false;
}
console.log(isPalindrome("A man, a plan, a canal: Panama")) // true
console.log(isPalindrome("race a car")) // false