Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Time: O(2n) where n is the length of the array, cause every letter was checked once
- # Space: O(1)
- '''
- i
- start = "RXXLRXRXL"
- end = "XXLXXRRLX"
- j
- L - R: x
- R - L: x
- L - L: i >= j -> i < j: x
- R - R: i <= j -> i > j: x
- "XXRXLXRXXX"
- "XXRLXXXXXR"
- '''
- class Solution(object):
- def canTransform(self, start, end):
- """
- :type start: str
- :type end: str
- :rtype: bool
- """
- i = j = 0
- if start.replace("X","") != end.replace('X',""):
- return False
- n = len(start)
- for i in range(len(start)):
- if start[i] != 'X':
- while j < n and end[j] == 'X':
- j += 1
- if start[i] == 'L':
- if i < j:
- return False
- else:
- if i > j:
- return False
- j += 1
- return True
Add Comment
Please, Sign In to add comment