Leetcode-5076 Greatest Common Divisor of Strings(字符串的最大公因子)

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 
 3 class Solution
 4 {
 5     public:
 6         bool judge(string s1,string s2)
 7         {
 8             if(s2.size()%s1.size()!=0)
 9                 return false;
10             for(int i = 0;i < s2.size();i ++)
11             {
12                 if(s1[i%s1.size()]!=s2[i])
13                     return false;
14             }
15             return true;
16         }
17         string gcdOfStrings(string str1, string str2)
18         {
19             string rnt = "";
20             string tmp;
21             if(str1.size()<str2.size())
22             {
23                 string t = str1;
24                 str1 = str2;
25                 str2 = t;
26             }
27             for(int i = 0;i < str2.size();i ++)
28             {
29                 tmp += str2[i];
30                 if(judge(tmp,str1)&&judge(tmp,str2))
31                 {
32                     rnt = tmp;
33                 }
34             } 
35             return rnt;
36         }
37 };

 

posted @ 2019-06-02 12:04  Asurudo  阅读(337)  评论(0编辑  收藏  举报