GCD string LeetCode--
Topics are as follows:
For strings S and T, only S = T + ... + time T (T connected to itself one or more times), we identified "T S can be divisible."
Returns the longest string X, X can meet the requirements, and X can be divisible divisible str1 str2.
Example 1:
Input: str1 = “ABCABC”, str2 = “ABC”
Output: “ABC”
Example 2:
Input: str1 = “ABABAB”, str2 = “ABAB”
Output: “AB”
Example 3:
Input: str1 = “LEET”, str2 = “CODE”
Output: “”
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/greatest-common-divisor-of-strings
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
I give the answer:
string gcdOfStrings(string str1, string str2) {
string s1 = str1; string s2 = str2;//防止swap后,swap之后的新str1和形参str1傻傻分不清
int size1 = s1.length(); int size2 = s2.length(); int cFactor = 1;//commonFactor最大公因子
if (size1 > size2) swap(s1, s2);//整明白那个长那个短,标注出来
size1 = s1.length(); size2 = s2.length();
vector<int> factor;
for (size_t i = 1; i <=size1; i++)
{
if (size1 % i == 0 && size2 % i == 0) {
factor.push_back(i);
cFactor = i;
}
}//找出最大公因数cFactor和每个公因数
int n = factor.size();//公因数个数
for (size_t k = 1; k <= n; k++)
{
int fac= factor[n - k];
string t;//在此公因数作为长度截取较短的那一个
for (size_t i = 0; i < fac; i++) t.push_back( s1[i]);
int i = 0; int count=0;
while (i<size2)
{
if (s2[i] == t[(i + fac) % fac]) ++i;
else
{
count = -4; //随便赋个好区分的值
break;
}
}
if (count !=-4 ) count = 1;
i = fac;
if (i == size1 && count!=-4) return t; //正好是短的那个字符串
else
{
while (i < size1)
{
if (s1[i] == t[i % fac]) i++;
else {
count = -4;
break;
}
}
}
if (count != -4 ) count++;
if (count == 2) return t;
}
return "";
}
Efficiency is very good.
The official answer:
bool check(string t,string s){//检查string片段t是否能组成字符串s
int lenx = (int)s.length() / (int)t.length();
string ans = "";
for (int i = 1; i <= lenx; ++i){
ans = ans + t;
}
return ans == s;
}
string gcdOfStrings(string str1, string str2) {//gcd Greatest Common Divisor 最大公约数,不是暗喻
int len1 = (int)str1.length(), len2 = (int)str2.length();
for (int i = min(len1, len2); i >= 1; --i){ // 从长度的最大可能开始枚举
if (len1 % i == 0 && len2 % i == 0){//如果不是因子直接pass
string X = str1.substr(0, i);
if (check(X, str1) && check(X, str2)) return X;
}
}
return "";
}
//可取之处:直接判断公因子,并且把string当成整体来看,碎片拼接而成
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/solution/zi-fu-chuan-de-zui-da-gong-yin-zi-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- Which uses the min () function is selected size.
- substr () function returns a character string part, wherein the usage of substr substr (string, start, lenth), when the blank is a lenth line and in the end.
- A string used in the object can be a. (Start, lenth).
bool check(string t,string s){//传递的是一个固定的最大公约数,并且只传递比较一次
int lenx = (int)s.length() / (int)t.length();
string ans = "";
for (int i = 1; i <= lenx; ++i){
ans = ans + t;
}
return ans == s;
}
string gcdOfStrings(string str1, string str2) {
int len1 = (int)str1.length(), len2 = (int)str2.length();
string T = str1.substr(0, __gcd(len1,len2)); /* __gcd() 为c++自带的求最大公约数的函数。leetcode的题解是这么说的,
但我死活没调用出来__gcd()函数,不过不是重点*/
if (check(T, str1) && check(T, str2)) return T;
return "";
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/solution/zi-fu-chuan-de-zui-da-gong-yin-zi-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Best of! B Department of Mathematics see Mongolian students in mathematics law!
As long as string1 + string2 == string2 + string1 on the line.
Specific methods can be demonstrated by cutting method, string1 + string2 string2 + string1 and cut into pieces of the greatest common divisor size, then its corresponding respective small pieces are all equal to prove equal to prove.
string gcdOfStrings(string str1, string str2) {
if (str1 + str2 != str2 + str1) return "";
return str1.substr(0, __gcd((int)str1.length(), (int)str2.length())); // __gcd() 为c++自带的求最大公约数的函数
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/solution/zi-fu-chuan-de-zui-da-gong-yin-zi-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。