This article to share the content is about PHP removal of the fewest characters into palindrome, has a certain reference value, the need for friends can refer to
$s = "; for ($i =0; $i <15; $i + +) { $s. =CHR (rand (97, 122));} $s = $string = strtoupper ($s); Echo $string;
This is a randomly generated string
A string, removing the fewest characters to make it a palindrome such as ABBA Ababa a are palindrome// ABSDFDA Remove the fewest characters and form a palindrome into a adfda// Focus on the first and last character of the next string must be the same
Delete the value of a position in a string function Delete_str_pos ($str, $pos) {$s = Str_split ($STR); Unset ($s [$pos]); Return implode ("", array_values ($s));} The judgment is not a palindrome string function Is_huichuan ($s) {if (strlen ($s) ==1) {return true; }else {$o = strlen ($s)/2; $two = Floor ($o); $a = substr ($s, 0, $two); if (Is_float ($o)) {$b =substr ($s, $two + 1); }else {$b =substr ($s, $two); } if (Strtolower ($a) ==strtolower (Strrev ($b))) {return true; }} return false;} The 2nd method of judging palindrome function Is_huiwen ($s) {if (Strtoupper ($s) = = Strtoupper (Implode ("", Array_reverse (Str_split ($s)))) { return true; } return false;} function Delete_pos ($str, $min, $max) {$s = Str_split ($STR); if ($max > strlen ($str)) {$max = strlen ($STR); } for ($i = $min; $i < $max; $i + +) {unset ($s [$i]); } Return Implode ("", array_values ($s));} function Method1 ($string) {$end =-1;for ($i =0; $i <strlen ($string), $i + +) {if (Is_huichuan ($string)) {Header ("Content-type:text/ht Ml;charset=utf-8 "); echo "<br/>"; echo "is a palindrome"; Break } $pos = Strrpos ($string, $string [$i], $end); if (Is_numeric ($pos)) {//represents the first directly deleted character if ($pos = = $i) {$string = Delete_str_pos ($string, $pos); Remove string $i--; }elseif ($pos = = strlen ($string)-1) {//This time is the last string for palindrome $end = $end-1; }else {$string = Delete_pos ($string, $pos +1,strlen ($string)-$i); }}else {//If the character that was not found directly deleted $i is incorrect $string = Delete_str_pos ($string, $i);//Remove string $i--; } echo "<br/>"; Echo $string; }}