aboutsummaryrefslogtreecommitdiff
path: root/swap_adjacent_lr_string.py
diff options
context:
space:
mode:
Diffstat (limited to 'swap_adjacent_lr_string.py')
-rw-r--r--swap_adjacent_lr_string.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/swap_adjacent_lr_string.py b/swap_adjacent_lr_string.py
new file mode 100644
index 0000000..e0ab186
--- /dev/null
+++ b/swap_adjacent_lr_string.py
@@ -0,0 +1,38 @@
+# 777. Swap Adjacent in LR String
+
+"""
+in a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a
+move consists of either replacing one occurrence of "XL" with "LX", or
+replacing one occurrence of "RX" with "XR". given the starting string 'start'
+and the ending string 'end', return true if and only if there exists a
+sequence of moves to transform 'start' to 'end'
+"""
+
+
+class Solution(object):
+ def canTransform(self, start, end):
+ """
+ :type start: str
+ :type end: str
+ :rtype: bool
+ """
+ if start.replace("X", "") != end.replace("X", ""):
+ return False
+ n = len(start)
+ l_start = [i for i in range(n) if start[i] == "L"]
+ l_end = [i for i in range(n) if end[i] == "L"]
+ r_start = [i for i in range(n) if start[i] == "R"]
+ r_end = [i for i in range(n) if end[i] == "R"]
+ for i, j in zip(l_start, l_end):
+ if i < j:
+ return False
+ for i, j in zip(r_start, r_end):
+ if i > j:
+ return False
+ return True
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.canTransform(start="RXXLRXRXL", end="XRLXXRRLX"))
+ print(obj.canTransform(start="X", end="L"))