其他分享
首页 > 其他分享> > 力扣第1074题 字符串的最大公因子

力扣第1074题 字符串的最大公因子

作者:互联网

力扣第1074题 字符串的最大公因子

class Solution {
    public:
    bool check(string t, string str)
    {
        int num = str.size() / t.size();
        string temp = "";
        for (int i = 0; i < num; i++)
        {
            temp += t;
        }
        return temp == str;
    }
    string gcdOfStrings(string str1, string str2)
    {
        int len1 = str1.size(), len2 = str2.size();
        if (len1 > 0 && len2 > 0)
        {
            int num1 = len1, num2 = len2;
            if (num1 < num2)
            {
                num1 = num1 + num2;
                num2 = num1 - num2;
                num1 = num1 - num2;
            }
            int temp = 0;
            while (num2 != 0)
            {
                temp = num1 % num2;
                num1 = num2;
                num2 = temp;
            }
            string subStr = str1.substr(0, num1);
            if (check(subStr, str1) && check(subStr, str2))
            {
                return subStr;
            }
        }
        return "";
    }
};
    

标签:num1,num2,int,1074,力扣,temp,字符串,size,string
来源: https://www.cnblogs.com/woodjay/p/12483745.html