-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidpalindrome.js
More file actions
45 lines (41 loc) · 1.12 KB
/
validpalindrome.js
File metadata and controls
45 lines (41 loc) · 1.12 KB
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
/*
Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
exceptions:
" " true
".," true
*/
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
let output = false;
if (s.length > 1) {
let strRev = s.split("").reverse().join("");
if (s == strRev) {
output = true
} else {
let str = s.match(/[a-zA-Z0-9]+/g);
if (str?.length > 0) {
let newstr = str.join("").toLowerCase();
let strRev = newstr.toLowerCase().split("").reverse()?.join("");
console.log(newstr, strRev)
if (newstr == strRev) { output = true }
} else {
// handling exceptions (.,)
output = true
}
}
} else if (s.length == 1) {
output = true
}
return output;
};