Created
December 13, 2020 04:26
-
-
Save lasverg/9fb71ecd1ad9db34d10ec92e6d9d961d to your computer and use it in GitHub Desktop.
Palindrom: check if input is palindrom | recursive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isPalRec = (string, start, end) => { | |
// If there is only one character | |
if (start === end) { | |
return true; | |
} | |
// If first and last | |
// characters do not match | |
if (string[start] !== string[end]) { | |
return false; | |
} | |
// If there are more than two | |
// characters, check if middle | |
// substring is also palindrome or not. | |
if (start < end + 1) { | |
return isPalRec(string, start + 1, end - 1); | |
} | |
return true; | |
}; | |
const isPalindrome = (string) => { | |
const length = string.length; | |
// An empty string is | |
// considered as palindrome | |
if (length === 0) { | |
return true; | |
} | |
return isPalRec(string, 0, length - 1); | |
}; | |
function log() { | |
console.log(isPalindrome("nitin")); | |
} | |
log(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment