-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPalindrome.js
More file actions
executable file
·50 lines (37 loc) · 1.53 KB
/
isPalindrome.js
File metadata and controls
executable file
·50 lines (37 loc) · 1.53 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
46
47
48
49
50
// function to check palindrome using recursion
// what is palindrome ?
// A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).
// For example, "racecar", "madam", and
// "A man, a plan, a canal: Panama" are all palindromes.
function isPalindrome(str) {
// helper function to clean the string
function cleanString(s) {
return s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
}
// cleaned string
const cleanedStr = cleanString(str);
// recursive function to check palindrome
function checkPalindrome(s, left = 0, right = s.length - 1) {
// base case
if (left >= right) {
return true;
}
// if characters do not match
if (s[left] !== s[right]) {
return false;
}
// recursive case
return checkPalindrome(s, left + 1, right - 1);
}
return checkPalindrome(cleanedStr, 0, cleanedStr.length - 1);
}
// time complexity: O(n)
// space complexity: O(n) due to call stack
// test the function
console.log(isPalindrome("racecar")); // Output: true
console.log(isPalindrome("hello")); // Output: false
console.log(isPalindrome("A man, a plan, a canal: Panama")); // Output: true
console.log(isPalindrome("No 'x' in Nixon")); // Output: true
console.log(isPalindrome("JavaScript")); // Output: false
console.log(isPalindrome("Madam")); // Output: true
console.log(isPalindrome("12321")); // Output: true