Remove the maximum of the same substring from two strings

Source: Internet
Author: User

//*************************************************************************//Topic Requirements: 4, remove the maximum of two strings in the same substring. //************************************************************************* Public classsearchmaxsamestring { Public Static voidMain (string[] args) {String str1=NULL; String str2=NULL; String Maxstr=NULL; //Enter two stringsScanner input =NewScanner (system.in); System.out.print ("Please enter string str1:"); STR1=Input.nextline (); System.out.print ("Please enter string str2:"); STR2=Input.nextline (); //separates the two strings entered into aString shortstr = Str1.length () > Str2.length ()?str2:str1; String Longstr= Shortstr.equals (str1)?str2:str1; //call Maxserach to get the maximum stringMaxstr =maxstring (Shortstr, LONGSTR); //Output StringSYSTEM.OUT.PRINTLN ("Maximum same substring is:" +maxstr);    Input.close (); }    //define a static method Maxserach to get the maximum string     Public Staticstring maxstring (String shortstr, String longstr) {//defines an empty string to store the maximum stringString maxstr = ""; //The maximum same string is obtained by the short string in two strings         for(inti = 0; I < shortstr.length (); i++) {            //from the go, the truncated substring is held in the temporary string Temp1String Temp1 =shortstr.substring (i);  for(intj = Temp1.length ()-1; J >= 0; j--) {                //The obtained temporary substring Temp1 is then intercepted from the back, to get all substrings stored in the Temp2String temp2 = temp1.substring (0, J); //If there is a substring, and the substring has the largest length, it is paid to Maxstr                if(Longstr.indexof (TEMP2)! =-1 && temp2.length () >maxstr.length ()) {Maxstr=Temp2; }            }        }        //back to Maxstr        returnMaxstr; }}// *************************************************************************//Test Results://Please enter a string STR1:FFDSAJOIFWWQN3342NKJNF23N42FSD//Please enter a string STR2:FDAFIOENRJOIFWWQN3342NKJNF23N4023JFS,ADFJ//The maximum same substring is: Joifwwqn3342nkjnf23n4// *************************************************************************

The code that asks for the most identical string has forgotten to refer to the man's, and I can't!

Remove the maximum of the same substring from two strings

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.