Title: Enter two strings, calculate the length of the maximum common string of two strings, and output , characters are case insensitive
Eg: input ABCDE xxxbcyyy, Output 2.
Full Java code:
ImportJava.util.*; Public classMain { Public Static voidMain (String arg[]) {Scanner s=NewScanner (system.in); String str1=S.next (); String str2=S.next (); S.close (); String Maxstr,minstr; if(Str1.length () >str2.length ()) {Maxstr=STR1; Minstr=str2; } Else{maxstr=STR2; Minstr=str1; } intmax=maxstr.length (); intMin=minstr.length ();//System.out.println (maxstr+ "" +minstr+ "" +max+ "" +min "); intResult=0; OK: for(intl=min;l>0;l--){ for(inti=0;i<=max-l;i++){ for(intj=0;j<=min-l;j++){ if(Maxstr.regionmatches (true, I, Minstr, J, L)) {Result=l;//System.out.println (L + "" +i+ "" +j); BreakOK; }}}} System.out.println (Result); } }
Learning Point One: Use Java tags to jump out of multiple loops;
Learning Point Two: Flexible use of the String.regionmatches method, to determine whether the substring of the two string region is equal, refer to the Java API documentation as follows.
Regionmatches
regionmatches (Boolean ignoreCase, int toffset, String Other, int ooffset, int len)
-
Tests whether two string regions are equal.
Compares the substring of this string object with the substring of the parameter other. If these two substrings represent the same sequence of characters, the result is true, and the case is ignored if and only if ignoreCase is true. The substring of this string object to compare starts at index Toffset and is lenlong. The substring of the other to be compared starts at index Ooffset and is lenin length. The result is falsewhen and only if at least one of the following is true:
- toffset is negative.
- ooffset is negative.
- Toffset+len is greater than the length of this String object.
- Ooffset+len is greater than the length of another parameter.
- ignoreCase is falseand there is a non-negative integer Kthat is less than len , i.e.:
- ignoreCase is true, and there is a non-negative integer Kthat is less than Len , which is:
Character.tolowercase (This.charat (toffset+k))! =
And:
Character.touppercase (This.charat (toffset+k))! = Character.touppercase (Other.charat (ooffset+k))
-
-
-
-
-
Parameters:
-
ignoreCase -If it is
true , the case is ignored when comparing characters.
-
toffset -The starting offset of the neutron region of this string.
-
other -string parameter.
-
toffset -The starting offset of the neutron region of the string parameter.
-
len -the number of characters to compare.
-
Return:
-
returns if the specified sub-range of this string matches the specified sub-region of the string parameter
true ;
false Whether an exact match or case is considered depends on the
ignoreCase parameter.
Common string Calculation--string.regionmatches methods & Java Tags