For two strings s and t, we say “t divides s” if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times).
Given two strings str1 and str2, return the largest string x such that x divides both str1 and 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: ""
 
Constraints:
1 <= str1.length, str2.length <= 1000str1andstr2consist of English uppercase letters.
Thought:
-  
定义一个
check函数,用于判断一个字符串t是否是另一个字符串s的因子。如果s可以由多个t拼接而成,则返回true,否则返回false。 -  
在主函数
gcdOfStrings中,首先计算出两个字符串的长度len1和len2。 -  
然后取出
str1的前gcd(len1, len2)个字符作为字符串T。 -  
判断
T是否是str1和str2的因子,如果是,则返回T,否则返回空字符串。 
AC:
/*
 * @lc app=leetcode.cn id=1071 lang=cpp
 *
 * [1071] 字符串的最大公因子
 */
// @lc code=start
class Solution {
    bool check(string t, string s)
    {
        int lenx = (int)s.length() / (int)t.length();
        string ans = "";
        for(int i = 1; i <= lenx; i++)
        {
            ans += t;
        }
        return ans == s;
    }
public:
    string gcdOfStrings(string str1, string str2) {
        int len1 = (int)str1.length(), len2 = (int)str2.length();
        string T = str1.substr(0, __gcd(len1, len2));
        if(check(T, str1) && check(T, str2))
            return T;
        return "";
    }
};
// @lc code=end
 

 此外,官方题解中有一种数学做法,更为便捷!
/*
 * @lc app=leetcode.cn id=1071 lang=cpp
 *
 * [1071] 字符串的最大公因子
 */
// @lc code=start
class Solution {
public:
    string gcdOfStrings(string str1, string str2) {
        if(str1 + str2 != str2 + str1)
            return "";
        return str1.substr(0, __gcd((int)str1.length(), (int)str2.length()));
    }
};
// @lc code=end
 

很夸张,用时 0ms!!!
数学,yyds!说是
Supplement:
 __gcd是C++ STL中的函数,用于求两个数的最大公约数。它的使用方法如下:
-  
首先需要包含头文件。
 -  
调用
__gcd函数,传入两个参数,返回值即为这两个数的最大公约数。 
示例代码如下:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
    int a = 18, b = 24;
    int gcd_result = __gcd(a, b);
    cout << "The gcd of " << a << " and " << b << " is " << gcd_result << endl;
    return 0;
}
 
运行结果为:
The gcd of 18 and 24 is 6
 
注意,由于__gcd是C++ STL中的函数,因此需要编译时加上参数-std=c++11或更高版本,否则编译器可能会报错。
C++中的substr()函数用于获取字符串的子字符串。具体用法如下:
string substr (size_t pos, size_t len) const;
 
参数说明:
pos:起始位置,即从第几个字符开始截取,下标从0开始。len:要截取的字符数。
返回值:
- 返回一个新的字符串,表示从原字符串中截取出的子字符串。
 
示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str = "Hello World!";
    string s1 = str.substr(0, 5); // 从第0个字符开始截取5个字符
    string s2 = str.substr(6); // 从第6个字符开始截取到字符串结尾
    cout << s1 << endl; // 输出:Hello
    cout << s2 << endl; // 输出:World!
    
    return 0;
}
 
C++中的substr()函数用于获取字符串的子字符串。具体用法如下:
string substr (size_t pos, size_t len) const;
 
参数说明:
pos:起始位置,即从第几个字符开始截取,下标从0开始。len:要截取的字符数。
返回值:
- 返回一个新的字符串,表示从原字符串中截取出的子字符串。
 
示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str = "Hello World!";
    string s1 = str.substr(0, 5); // 从第0个字符开始截取5个字符
    string s2 = str.substr(6); // 从第6个字符开始截取到字符串结尾
    cout << s1 << endl; // 输出:Hello
    cout << s2 << endl; // 输出:World!
    
    return 0;
}
                


















