From 867236519563387f44ad282089eb8f65761f4d07 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Sat, 22 Aug 2026 09:01:19 +0900 Subject: [PATCH] step1,2 --- 0777.Swap-Adjacent-in-LR-String/memo.md | 20 +++++++++++++++ 0777.Swap-Adjacent-in-LR-String/step1.py | 31 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 0777.Swap-Adjacent-in-LR-String/memo.md create mode 100644 0777.Swap-Adjacent-in-LR-String/step1.py diff --git a/0777.Swap-Adjacent-in-LR-String/memo.md b/0777.Swap-Adjacent-in-LR-String/memo.md new file mode 100644 index 0000000..70ce178 --- /dev/null +++ b/0777.Swap-Adjacent-in-LR-String/memo.md @@ -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 diff --git a/0777.Swap-Adjacent-in-LR-String/step1.py b/0777.Swap-Adjacent-in-LR-String/step1.py new file mode 100644 index 0000000..071c102 --- /dev/null +++ b/0777.Swap-Adjacent-in-LR-String/step1.py @@ -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