Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions 0777.Swap-Adjacent-in-LR-String/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 777. Swap Adjacent in LR String

## step1
最初 *.replace("X", "")を比較することを考えたが、L, Rの移動が一方に制限されているため誤り。

pointerでscanすることで解決。

## step2

https://leetcode.com/problems/swap-adjacent-in-lr-string/solutions/6970523/python-simple-two-pointers-by-rnotappl-yeij/?envType=problem-list-v2&envId=7p55wqm

同じ解法。ただし、こちらはwhileの条件が and でループの前にXの個数の一致を確認している。

これ以外の解法はない?

https://leetcode.com/problems/swap-adjacent-in-lr-string/solutions/2047353/python-on-with-comments-and-reasonings-b-kkw3/?envType=problem-list-v2&envId=7p55wqm
インデックスを全て保存する

## step3
TODO
31 changes: 31 additions & 0 deletions 0777.Swap-Adjacent-in-LR-String/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def canTransform(self, start: str, result: str) -> bool:
if len(start) != len(result):
return False
if not start:
return True

index_start = 0
index_result = 0

while index_start < len(start) or index_result < len(result):
while index_start < len(start) and start[index_start] == "X":
index_start += 1
while index_result < len(result) and result[index_result] == "X":
index_result += 1

if index_start == len(start) or index_result == len(result):
return index_start == index_result

if start[index_start] != result[index_result]:
return False

if start[index_start] == "L" and index_start < index_result:
return False
if start[index_start] == "R" and index_result < index_start:
return False

index_start += 1
index_result += 1

return True