|
| 1 | +--- |
| 2 | +title: LC1071. 字符串的最大公因子 greatest-common-divisor-of-strings |
| 3 | +date: 2025-08-31 |
| 4 | +categories: [Leetcode-75] |
| 5 | +tags: [leetcode, Leetcode-75, string] |
| 6 | +published: true |
| 7 | +--- |
| 8 | + |
| 9 | +# LC1071. 字符串的最大公因子 |
| 10 | + |
| 11 | +对于字符串 s 和 t,只有在 s = t + t + t + ... + t + t(t 自身连接 1 次或多次)时,我们才认定 “t 能除尽 s”。 |
| 12 | + |
| 13 | +给定两个字符串 str1 和 str2 。返回 最长字符串 x,要求满足 x 能除尽 str1 且 x 能除尽 str2 。 |
| 14 | + |
| 15 | +示例 1: |
| 16 | + |
| 17 | +输入:str1 = "ABCABC", str2 = "ABC" |
| 18 | +输出:"ABC" |
| 19 | +示例 2: |
| 20 | + |
| 21 | +输入:str1 = "ABABAB", str2 = "ABAB" |
| 22 | +输出:"AB" |
| 23 | +示例 3: |
| 24 | + |
| 25 | +输入:str1 = "LEET", str2 = "CODE" |
| 26 | +输出:"" |
| 27 | + |
| 28 | + |
| 29 | +提示: |
| 30 | + |
| 31 | +1 <= str1.length, str2.length <= 1000 |
| 32 | +str1 和 str2 由大写英文字母组成 |
| 33 | + |
| 34 | + |
| 35 | +# v1-暴力 |
| 36 | + |
| 37 | +## 思路 |
| 38 | + |
| 39 | +我们先用暴力的方法。 |
| 40 | + |
| 41 | +直接尝试 min(len(s1), min(s2)),用二者最短的直接遍历尝试。 |
| 42 | + |
| 43 | +可以从短到长,当然,从长到短可以直接结束。 |
| 44 | + |
| 45 | +## 实现 |
| 46 | + |
| 47 | +```java |
| 48 | +public String gcdOfStrings(String str1, String str2) { |
| 49 | + int n1 = str1.length(); |
| 50 | + int n2 = str2.length(); |
| 51 | + |
| 52 | + int min = Math.min(n1, n2); |
| 53 | + //遍历 |
| 54 | + for(int i = min; i >= 0; i--) { |
| 55 | + int len = i+1; |
| 56 | + // 无法整除 |
| 57 | + if(n1 % len != 0 || n2 % len != 0) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + // 二者都可以被整除 |
| 62 | + String item = str1.substring(0, i+1); |
| 63 | + int times1 = n1 / len; |
| 64 | + |
| 65 | + if(!repeat(item, times1).equals(str1)) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + int times2 = n2 / len; |
| 69 | + if(!repeat(item, times2).equals(str2)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + return item; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + return ""; |
| 79 | + } |
| 80 | + |
| 81 | + private String repeat(String item, int times) { |
| 82 | + StringBuilder buffer = new StringBuilder(); |
| 83 | + |
| 84 | + for(int i = 0; i < times; i++) { |
| 85 | + buffer.append(item); |
| 86 | + } |
| 87 | + |
| 88 | + return buffer.toString(); |
| 89 | + } |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +## 效果 |
| 94 | + |
| 95 | +1ms 击败 81.51% |
| 96 | + |
| 97 | +## 反思 |
| 98 | + |
| 99 | +当然,看到 gcd 让我想到了数学定理。 |
| 100 | + |
| 101 | +这一题肯定有更好的解法。 |
| 102 | + |
| 103 | +# v2-数学方法 |
| 104 | + |
| 105 | +## 思路 |
| 106 | + |
| 107 | +gcd 我们可以很自然的想到数学欧几里得定理。 |
| 108 | + |
| 109 | +1)检查 str1 + str2 == str2 + str1,否则返回 "" |
| 110 | + |
| 111 | +2)求 len(str1) 和 len(str2) 的 GCD,记为 g |
| 112 | + |
| 113 | +3)返回 str1[:g] |
| 114 | + |
| 115 | +## 疑问 |
| 116 | + |
| 117 | +1) 为什么要求 str1 + str2 == str2 + str1? |
| 118 | + |
| 119 | +假设有一个最大公因子字符串 X,满足: |
| 120 | + |
| 121 | +``` |
| 122 | +str1 = X * m (重复 m 次) |
| 123 | +str2 = X * n (重复 n 次) |
| 124 | +``` |
| 125 | + |
| 126 | +那么把两个字符串拼接起来: |
| 127 | + |
| 128 | +``` |
| 129 | +str1 + str2 = (X*m) + (X*n) = X*(m+n) |
| 130 | +str2 + str1 = (X*n) + (X*m) = X*(n+m) |
| 131 | +``` |
| 132 | + |
| 133 | +可以得到二者相等,进而: |
| 134 | + |
| 135 | +``` |
| 136 | +str1 + str2 = X*(m+n) = X*(n+m) = str2 + str1 |
| 137 | +``` |
| 138 | + |
| 139 | +这是存在公因子的必要条件。 |
| 140 | + |
| 141 | +2) 如何求 2 个数的 gcd? |
| 142 | + |
| 143 | +``` |
| 144 | +gcd(a,b)=gcd(b,a%b) |
| 145 | +``` |
| 146 | + |
| 147 | +其中 % 表示取余数。 |
| 148 | + |
| 149 | +直到 b = 0,此时 a 就是最大公约数。 |
| 150 | + |
| 151 | +也就是 |
| 152 | + |
| 153 | +```java |
| 154 | +public static int gcd(int a, int b) { |
| 155 | + while (b != 0) { |
| 156 | + int temp = b; |
| 157 | + b = a % b; |
| 158 | + a = temp; |
| 159 | + } |
| 160 | + return a; |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## 实现 |
| 165 | + |
| 166 | +```java |
| 167 | +public static int gcd(int a, int b) { |
| 168 | + while (b != 0) { |
| 169 | + int temp = b; |
| 170 | + b = a % b; |
| 171 | + a = temp; |
| 172 | + } |
| 173 | + return a; |
| 174 | + } |
| 175 | + |
| 176 | + public String gcdOfStrings(String str1, String str2) { |
| 177 | + if(!(str1+str2).equals(str2+str1)) { |
| 178 | + return ""; |
| 179 | + } |
| 180 | + |
| 181 | + // gcd |
| 182 | + int len1 = str1.length(); |
| 183 | + int len2 = str2.length(); |
| 184 | + int len = gcd(len1, len2); |
| 185 | + |
| 186 | + return str1.substring(0, len); |
| 187 | + } |
| 188 | + |
| 189 | +``` |
| 190 | + |
| 191 | +## 效果 |
| 192 | + |
| 193 | +1ms 击败 81.51% |
| 194 | + |
| 195 | +时间复杂度低,O(n) |
| 196 | + |
| 197 | +## 反思 |
| 198 | + |
| 199 | +为什么不是 100%? |
| 200 | + |
| 201 | + |
| 202 | +# 参考资料 |
| 203 | + |
| 204 | +https://leetcode.cn/problems/greatest-common-divisor-of-strings/description/?envType=study-plan-v2&envId=leetcode-75 |
0 commit comments