Leetcode-1071-GCD-of-Strings
🧠 思路总结
- 题目本质是在找一个最短重复单元,使它能重复拼接得到 str1 和 str2。
- 如果两个字符串来自同一个重复单元,那么
str1 + str2和str2 + str1的结果一定完全相同。 - 在确认结构一致后,问题就转化为:这个重复单元的长度是多少(最大公约数(GCD))。
❗ 易错点 / 小坑
- 最大公约字符串一定是前缀,不需要考虑中间子串
- gcd 方法本质是欧几里得算法,递归结束条件是 b == 0
✅ 代码(Java)
1 | |
Leetcode-1071-GCD-of-Strings
https://powersixxx.github.io/leetcode-blog/2026/01/26/Leetcode-1071-GCD-of-Strings/