PHP removes the fewest characters into a palindrome string

Source: Internet
Author: User
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; }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.