Created
May 10, 2024 01:23
-
-
Save aklump/62ac991471d4157aae2fa55853242208 to your computer and use it in GitHub Desktop.
Count the total number of palindromes in a word.
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 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