Created
April 24, 2017 05:58
-
-
Save devd123/faf1a4fd4c03e936a59bd4945b663413 to your computer and use it in GitHub Desktop.
Find longest palindrome string in php
This file contains 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
<?php | |
/** | |
* find the longest palindrome in a string | |
* website : neerusite.wordpress.com | |
* Auther : Devdutt Sharma | |
*/ | |
function longestPalindrome($string = ''){ | |
$strArr = array(); | |
for($i=0; $i<strlen($string); $i++ ) | |
{ | |
$palindrome = true; | |
$offset = 1; | |
while($palindrome) | |
{ | |
$word = substr($string, $i-$offset, ($offset*2)+1 ); | |
if( $word == strrev($word) ) { | |
//print strlen($word) .' '. $word.'<br />'; | |
$strArr[$word] = strlen($word); | |
} else { | |
$palindrome = false; | |
} | |
$offset++; | |
} | |
} | |
$finArr = max($strArr); | |
echo $key = array_search ($finArr, $strArr); | |
} | |
longestPalindrome($string = "abcdcbadfddeegfdsrefgr"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment