# [LeetCode 75] DAY 2 - Greatest Common Divisor of Strings
## 題目:
For two strings `s` and `t`, we say "`t` divides `s`" if and only if `s = t + t + t + ... + 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`.
## 解題思考
- 兩字串連結若不相等,直接返回空字串
- 取字串的最大公約數(GCD)
- 依照最大公約數來截取字串
## 程式碼
```javascript
function gcd(a, b){
if (b === 0){
return a;
}
return gcd(b, a % b);
}
function gcdOfStrings(str1, str2) {
if (str1 + str2 !== str2 + str1) return '';
const lengthGCD = gcd(str1.length, str2.length);
return str1.slice(0, lengthGCD);
// return str.subString(0, lengthGCD);
}
```
## 延伸思考
- 使用 `slice()` 和 `subString()` 擷取字串有什麼差異?
## 相關資源
- [1071. Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/description/)