Created
August 14, 2016 02:08
-
-
Save chrisnatali/48402a69407a1abd89068c7e7efa80ef to your computer and use it in GitHub Desktop.
get palindrome function in javascript
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 getPalindromes(s) { | |
/* get all palindromes of the string s */ | |
var results = []; | |
for (var i = 0; i < s.length; i++) { | |
var start, end; | |
start = end = i; | |
//check 'odd' palindromes | |
while ( 0 <= start && end < s.length && s[start] == s[end] ) { | |
results.push(s.substring(start, end + 1)); | |
start--; | |
end++; | |
} | |
//check 'even' palindromes | |
start = i; | |
end = i + 1; | |
while ( 0 <= start && end < s.length && s[start] == s[end] ) { | |
results.push(s.substring(start, end + 1)); | |
start--; | |
end++; | |
} | |
} | |
return results; | |
} | |
function countPalindromes(s) { | |
/* count palindromes of the string s */ | |
var results = getPalindromes(s); | |
return results.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment