Skip to content

Instantly share code, notes, and snippets.

@aklump
Created May 10, 2024 01:23
Show Gist options
  • Save aklump/62ac991471d4157aae2fa55853242208 to your computer and use it in GitHub Desktop.
Save aklump/62ac991471d4157aae2fa55853242208 to your computer and use it in GitHub Desktop.
Count the total number of palindromes in a word.
function countPalindromes($word) {
$palindromes = 0;
$word_length = strlen($word);
for ($chunk_size = $word_length; $chunk_size > 0; --$chunk_size) {
for ($pos = 0; $pos <= ($word_length - $chunk_size); $pos++) {
$substr = substr($word, $pos, $chunk_size);
if ($substr === strrev($substr)) {
++$palindromes;
}
}
}
return $palindromes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment