推 sustainer123: 大師 08/03 11:20
http://i.imgur.com/OLvBn3b.jpg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.248 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1722654924.A.4DA.html
1460. Make Two Arrays Equal by Reversing Subarrays
## 思路
用Counter檢查兩字串出現char相不相同
一行easy
## Code
```python
class Solution:
def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
return Counter(target) == Counter(arr)
```
---
順便多了寫個一題
1071. Greatest Common Divisor of Strings
## 思路
兩字串長度做gcd, 再檢查兩字串是否可用該gcd string組成
## Code
```python
class Solution:
def gcdOfStrings(self, str1: str, str2: str) -> str:
def gcd(a, b):
while b:
a, b = b, a % b
return a
def is_concat(s, size):
for i in range(len(s)):
if s[i] != str1[i % size]:
return False
return True
max_len = gcd(len(str1), len(str2))
if is_concat(str1, max_len) and is_concat(str2, max_len):
return str1[:max_len]
return ''
```
--