1071. Greatest Common Factor of Strings
problem solving ideas
- Roll and divide
- Calculates the greatest common factor of the lengths of two strings
- Then intercept the string length
- Before using the rolling and dividing method, it is necessary to judge whether there is a greatest common factor
class Solution {
private int al(int length1, int length2){
if(length2 == 0){
return length1;
}else{
return al(length2,length1 % length2);
}
}
public String gcdOfStrings(String str1, String str2) {
if(!(str1 + str2).equals(str2 + str1)){
return "";
}
return str1.substring(0,al(str1.length(),str2.length()));
}
}