Guest User

Untitled

a guest
Feb 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. # Time: O(2n) where n is the length of the array, cause every letter was checked once
  2. # Space: O(1)
  3. '''
  4. i
  5. start = "RXXLRXRXL"
  6. end = "XXLXXRRLX"
  7. j
  8. L - R: x
  9. R - L: x
  10. L - L: i >= j -> i < j: x
  11. R - R: i <= j -> i > j: x
  12.  
  13.  
  14. "XXRXLXRXXX"
  15. "XXRLXXXXXR"
  16. '''
  17.  
  18. class Solution(object):
  19. def canTransform(self, start, end):
  20. """
  21. :type start: str
  22. :type end: str
  23. :rtype: bool
  24. """
  25. i = j = 0
  26. if start.replace("X","") != end.replace('X',""):
  27. return False
  28.  
  29. n = len(start)
  30. for i in range(len(start)):
  31. if start[i] != 'X':
  32. while j < n and end[j] == 'X':
  33. j += 1
  34. if start[i] == 'L':
  35. if i < j:
  36. return False
  37. else:
  38. if i > j:
  39. return False
  40. j += 1
  41. return True
Add Comment
Please, Sign In to add comment