Last active
August 29, 2015 14:13
-
-
Save leandrooriente/11e17aa650f90863cc80 to your computer and use it in GitHub Desktop.
Kpalíndromo em javascript usando recursividade
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
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