Re: [閒聊] 每日leetcode
2337. Move Pieces to Obtain a String
## 思路
two pointer
每次loop 檢查跳過`_`後遇到的字元
如果字元一樣, 就再檢查index
R: 右移 (idx_start <= idx_target
L: 左移 (idx_start >= idx_target
## Code
```python
class Solution:
def canChange(self, start: str, target: str) -> bool:
n = len(start)
i = j = 0
while True:
while i < n and start[i] == '_':
i += 1
while j < n and target[j] == '_':
j += 1
if i == n or j == n:
break
if start[i] != target[j]:
return False
if start[i] == 'L' and i < j:
return False
if start[i] == 'R' and i > j:
return False
i += 1
j += 1
return i == j == n
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 84.17.37.73 (香港)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1733403836.A.46F.html
推
12/05 21:08,
1年前
, 1F
12/05 21:08, 1F
討論串 (同標題文章)
完整討論串 (本文為第 1181 之 1548 篇):