Skip to content

Instantly share code, notes, and snippets.

@leandrooriente
Last active August 29, 2015 14:13
Show Gist options
  • Save leandrooriente/11e17aa650f90863cc80 to your computer and use it in GitHub Desktop.
Save leandrooriente/11e17aa650f90863cc80 to your computer and use it in GitHub Desktop.
Kpalíndromo em javascript usando recursividade
function kpalindrome (word, k){
var len = word.length,
lastCharIndex = len - 1;
if (len === 0 || len === 1) {
return true;
}
if (word[0] === word[lastCharIndex]) {
// Remove first and last characteres of the string
word = word.substr(0, lastCharIndex).substr(1);
return kpalindrome(word, junk);
} else if (k > 0) {
var splitLastChar = word.substr(0, lastCharIndex),
splitFirstChar = word.substr(1);
return ( kpalindrome( splitLastChar, --k ) || kpalindrome( splitFirstChar, --k ) );
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment