-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidPalindrome.js
More file actions
28 lines (22 loc) · 863 Bytes
/
validPalindrome.js
File metadata and controls
28 lines (22 loc) · 863 Bytes
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
/*
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.
*/
let s = "A man, a plan, a canal: Panama"
function validPalindrome(s){
//convert the string into lowercase
s = s.toLowerCase()
//Filtered string
let filteredString = ""
for(let i = 0; i<s.length; i++){
if(s[i].match(/[a-z0-9]/i)){ //to remove any alphanumeric characters
filteredString += s[i]
}
}
//reverse the string
let reverse = filteredString.split("").reverse().join("")
return reverse === filteredString
}
let result = validPalindrome()
console.log(result)