Skip to content

Instantly share code, notes, and snippets.

@lasverg
Created December 13, 2020 04:26
Show Gist options
  • Save lasverg/9fb71ecd1ad9db34d10ec92e6d9d961d to your computer and use it in GitHub Desktop.
Save lasverg/9fb71ecd1ad9db34d10ec92e6d9d961d to your computer and use it in GitHub Desktop.
Palindrom: check if input is palindrom | recursive
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