This is the first Ogawa 391 update, the first 421 Pian original
01 questions and look ready
Introduced today is LeetCode algorithm in title Easy level of 253 questions (overall title number is 1071 ). For strings S
and T
, if and only if S = T + ... + T
( T
when connected to its own 1 or more times), we say "T除S"
.
Returns the largest string X
that X
divide str1
, X
divide str2
.
E.g:
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
Input: str1 = "LEET", str2 = "CODE"
Output: ""
Note :
1 <= str1.length <= 1000
1 <= str2.length <= 1000
str1[i]
Andstr2[i]
are capital letters.
02 The first solution
Requires two strings is to identify the title str1
, str2
the greatest common divisor, i.e. str1
, str2
in the presence of both a substring, and repeated by the sub-string consisting of one or more times.
So, there is no greatest common divisor of two strings under what circumstances?
Both before and after each splicing, but not equal, then it is surely the greatest common divisor does not exist. For example, in the example str1 ="ABCABC"
, str2 ="ABC"
, str1
splicing str2
after becomes "ABCABCABC"
, str2
splicing str1
after becomes "ABCABCABC"
. And str1 ="LEET"
, str2 ="CODE"
, str1
stitching str2
after become "LEETCODE"
, str2
splicing str1
after become "CODELEET"
, both obviously are not equal, and certainly there is no common divisor.
That's how they find the greatest common divisor of it?
Idea : With a string split. Strings respectively different sub str1
and str2
split by String
the split
method implementation, the array of strings if the split is not left any element, indicates that the substring may be divisible. Found two smaller string length, the upper limit of the number of cycles, successively, taken from the back substring, taken out of the substring to split str1
and str2
, if the length of the array is optically resolved 0, then this sub string is the greatest common divisor.
public String gcdOfStrings(String str1, String str2) {
if (!(str1+str2).equals(str2+str1)) {
return "";
}
int n = Math.min(str1.length(), str2.length());
for (int i=n; i>=1; i--) {
String temp = str2.substring(0, i);
if (str2.split(temp).length == 0 &&
str1.split(temp).length == 0) {
return temp;
}
}
return "";
}
03 The second solution
A first solution and a similar idea, still by means of a string of properties, the use of the greatest common divisor to verify the replacement, by String
the replaceAll
methods.
public String gcdOfStrings2(String str1, String str2) {
if (!(str1+str2).equals(str2+str1)) {
return "";
}
int n = Math.min(str1.length(), str2.length());
for (int i=n; i>=1; i--) {
if (n%i != 0) {
continue;
}
String temp = str2.substring(0, i);
if(str1.replaceAll(temp,"").equals("") &&
str2.replaceAll(temp,"").equals("")) {
return temp;
}
}
return "";
}
04 A third solution
We can also think about the problem from a mathematical point of view.
Ideas: The length of the string seen two greatest common divisor of two integers, a single write algorithm for the greatest common divisor of two numbers, the greatest common divisor is calculated, taking two small string length, taken substring, substring length is the previous step to calculate the greatest common divisor, the substring is our final two majors of the string to be returned.
public String gcdOfStrings3(String str1, String str2) {
if (!(str1+str2).equals(str2+str1)) {
return "";
}
int len = str1.length();
int len2 = str2.length();
int gcd = GCD(len, len2);
if (len < len2) {
return str1.substring(0, gcd);
}
return str2.substring(0, gcd);
}
public int GCD(int a, int b) {
if (b == 0) {
return a;
}
return a % b == 0 ? b : GCD(b, a % b);
}
05 The fourth solution
We can also integrate with third solution used in the greatest common divisor of the recursive method, and string operations.
Two found that the larger the length of the string, all characters if the character string contains a large length smaller length of the string, with a smaller length of the string to be replaced in the larger substring, until one of them is empty string so far.
public String gcdOfStrings4(String str1, String str2) {
if (str1.length() < str2.length()) {
return gcdOfStrings4(str2, str1);
}
if (str2.isEmpty()) {
return str1;
}
if (!str1.contains(str2)) {
return "";
}
str1 = str1.replace(str2, "");
return gcdOfStrings4(str2, str1);
}
06 Summary
Thematic algorithm has been continuously day and more than seven months , the algorithm of feature articles 259 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures ] either a keyword to obtain a series of articles Collection .
That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!