Java implementation to find the current string maximum palindrome string code sharing _java

Source: Internet
Author: User

Look at the code first

public class Maxhuiwen {public static void main (string[] args) {//TODO auto-generated method stub String S
    = "ABB";
  Maxhuiwen (s);
    //1. Output palindrome string public static void Maxhuiwen (string s) {//Length of the storage string int. = S.length ();
    Store the longest palindrome string maxstring = "";  Traverses all substrings for the current string for (int i = 0; i < length; i++) {for (int j = i; j < length + 1; j +) {String S1
        = S.substring (i, j);
            If the current string is a palindrome string and is greater than the length of the maxstring, replace the current maxstring if (Huiwen (S1) && s1.length () > Maxstring.length ()) {
        maxstring = S1;
      }//system.out.println (S1);
    }//If the maxstring length is greater than or equal to 2, it is a palindrome string if (Maxstring.length () >= 2) {System.out.println (maxstring);
    else{System.out.println ("No palindrome string");
    }//2. Determine if a string is a palindrome public static Boolean Huiwen (String s) {Boolean flag = true;
    int length = S.length ();
    Char s1[] = S.tochararray (); Formerly, from the back, traversing the string,Compare for (int i = 0, j = length-1 I <= j; i++, j--) {if (S1[i]!= s1[j]) {flag = false;
  } return flag;
 }
   
}

One, the string of palindrome judgment

Judge whether a string is a palindrome

Problem description, given a string, such as String t= "Madam"; to determine if the string is a palindrome

Method One: 1, defines two string element pointers (note Java has no pointer concept), int right=t.length ()-1; int left=0;

2, that is, left starts from the left-hand side, right starts from the right-hand side, compares the meaning of the character is equal, if equal, will left++,right--; otherwise, direct return is not a palindrome

while (left<right) {

if (T.charat)!=t.charat (right) is return

false;

left++;

right--;

}

return true;

Code:

* 
   * 3: 
   * palindrome Judgement 
   * Problem Description: palindrome, English palindrome, refers to a reading and vice versa read the same string, such as Madam, I love me, 
   * method One: 
   * Analysis: Use two "pointers" Respectively from the string head and tail scan, if each "pointer" value is equal, this is a palindrome 
   /public 
  Boolean ispalindrome (String s) { 
    if (S==null) return 
      false ; 
    int left=0; 
    int Right=s.length ()-1; 
    while (left<right) { 
      if (S.charat)!=s.charat (right) is return 
        false; 
      left++; 
      right--; 
    } 
    return true; 
  } 


Method Two: Palindrome string such as "Madam", if it all reversed, or get its own "madam", in the two string comparison, if equal, it is a palindrome

1, to implement a function that reverses the string

 
 * * Implement a string inversion function 
/private string reverse (String str) { 
  string strresult= ""; 
  for (int i=str.length () -1;i>=0;i--) { 
    Strresult+=str.charat (i); 
  } 
  return strresult; 
} 
2, for the target string s, first invert it to Temp=reverse (s), and then in the Judgment temp.equals (s)/
* * The string is inverted, and then compared to the original string, if equal, it is palindrome, otherwise not
* Algorithm time complexity of O (n)/
public
Boolean isPalindrome2 (string s) {
string temp=reverse (s);
if (s.equals (temp)) return
true;
else return
false;


Two: How to find the maximum palindrome string for a given string

For example, given a string t= "Google", how to find its longest palindrome substring "goog"

1, the simplest direct idea is: Find the string of all substrings, and then determine whether each substring is a palindrome, and record, compare the maximum length of the palindrome, * algorithm time complexity of O (n^3)

 
 * * 4, to find the longest palindrome substring 
 * Problem Description: Given a string to find all of its substrings in the longest palindrome substring, such as Google string, the eldest son string for goog 
 * Analysis: 
 *1, the simplest direct idea is: to find all substrings string , and then determine whether each substring is a palindrome, and record, compare the maximum length of the palindrome 
 * algorithm time complexity of O (n^3)/public 
String longestPalindrome1 (string s) { 
  String Result=null; 
  String tempstring= ""; 
  Defines the length int max=0 of the longest palindrome substring 
  ; 
  Iterate through all the elements in the string for 
  (int i=0;i<s.length (); i++) { 
    //array subscript Pointer J begins with the string forward and traverses for 
    (int j=s.length () -1;j >i;j--) { 
      //judge every string time for palindrome 
      tempstring=s.substr (i, j+1); 
      If tempstring is a palindrome substring and its length (j-i+1) >max 
      if (ispalindrome (tempstring) && (j-i+1) >max) { 
        max=j-i +1; 
        Result=substring (i, j+1); 
      } 
    } 
  return result; 
} 

2, the second idea is for each character in the string t[i], judge

Even-length substring centered on t[i],t[i+1] is a palindrome

T[i] is an odd-length substring centered on a palindrome

public string longestPalindrome2 (string T) {string result=null; 
    The length of the maximum back character string is int max=0;  
      Iterate over each character, with each character centered to judge the parity-extended substring for (int i=0;i<t.length (); i++) {//define two array subscript pointers, i,i+1-centric sequence int pstart=i; 
      int pend=i+1; 
        while (pstart>=0&&pend<= (T.length ()-1) &&t.charat (Pstart) ==t.charat (pend)) {pStart--; 
      pend++; 
        ///If the length of the substring is >max, it is staged as the oldest child palindrome string length = (pEnd-1)-(pstart+1) -1=pend-pstart-1 if (Pend-pstart-1>max) { 
        max=pend-pstart-1; 
      Result=substring (pstart+1, pend-1+1); 
      ///I as the center, to determine whether the extended odd sequence is a palindrome string pstart=i-1; 
      pend=i+1; 
        while (pstart>=0&&pend<= (T.length ()-1) &&t.charat (Pstart) ==t.charat (pend)) {pStart--; 
      pend++; 
        } if (Pend-pstart-1>max) {max=pend-pstart-1; 
      Result=substrint (T, pstart+1, pend-1+1); 
  } return result; }

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.