Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/12/05 21:03), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串1181/1548 (看更多)
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
文章代碼(AID): #1dKQIyHl (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dKQIyHl (Marginalman)