Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/777.html
777. Swap Adjacent in LR String (Medium)
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 one string to the other.
Example:
Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX
Note:
1 <= len(start) = len(end) <= 10000
.- Both start and end will only consist of characters in
{'L', 'R', 'X'}
.
Companies:
Google
Related Topics:
Brainteaser
Solution 1.
-
class Solution { public boolean canTransform(String start, String end) { if (start == null || end == null || start.length() != end.length()) return false; List<Integer> indices1 = getLRIndices(start); List<Integer> indices2 = getLRIndices(end); if (indices1.size() != indices2.size()) return false; int size = indices1.size(); for (int i = 0; i < size; i++) { int index1 = indices1.get(i), index2 = indices2.get(i); if (index1 > 0 && index2 < 0 || index1 < 0 && index2 > 0 || index1 > index2) return false; } return true; } public List<Integer> getLRIndices(String str) { List<Integer> indices = new ArrayList<Integer>(); int length = str.length(); for (int i = 0; i < length; i++) { char c = str.charAt(i); if (c == 'L') indices.add(-i - 1); else if (c == 'R') indices.add(i + 1); } return indices; } } ############ class Solution { public boolean canTransform(String start, String end) { int n = start.length(); int i = 0, j = 0; while (true) { while (i < n && start.charAt(i) == 'X') { ++i; } while (j < n && end.charAt(j) == 'X') { ++j; } if (i == n && j == n) { return true; } if (i == n || j == n || start.charAt(i) != end.charAt(j)) { return false; } if (start.charAt(i) == 'L' && i < j || start.charAt(i) == 'R' && i > j) { return false; } ++i; ++j; } } }
-
// OJ: https://leetcode.com/problems/swap-adjacent-in-lr-string/ // Time: O(N) // Space: O(N) class Solution { public: bool canTransform(string start, string end) { int N = start.size(); string a = start, b = end; a.erase(remove(a.begin(), a.end(), 'X'), a.end()); b.erase(remove(b.begin(), b.end(), 'X'), b.end()); if (a != b) return false; for (int i = 0, j = 0; i < N; ++i) { if (start[i] == 'L') { while (end[j] != 'L') ++j; if (i < j++) return false; } } for (int i = 0, j = 0; i < N; ++i) { if (start[i] == 'R') { while (end[j] != 'R') ++j; if (i > j++) return false; } } return true; } };
-
class Solution: def canTransform(self, start: str, end: str) -> bool: n = len(start) i = j = 0 while 1: while i < n and start[i] == 'X': i += 1 while j < n and end[j] == 'X': j += 1 if i >= n and j >= n: return True if i >= n or j >= n or start[i] != end[j]: return False if start[i] == 'L' and i < j: return False if start[i] == 'R' and i > j: return False i, j = i + 1, j + 1 ############ class Solution(object): def canTransform(self, start, end): """ :type start: str :type end: str :rtype: bool """ i, j = 0, 0 N = len(start) while i < N and j < N: while i < N - 1 and start[i] == 'X': i += 1 while j < N - 1 and end[j] == 'X': j += 1 if start[i] != end[j]: return False if start[i] == 'L' and i < j: return False if start[i] == 'R' and i > j: return False i += 1 j += 1 return True
-
func canTransform(start string, end string) bool { n := len(start) i, j := 0, 0 for { for i < n && start[i] == 'X' { i++ } for j < n && end[j] == 'X' { j++ } if i == n && j == n { return true } if i == n || j == n || start[i] != end[j] { return false } if start[i] == 'L' && i < j { return false } if start[i] == 'R' && i > j { return false } i, j = i+1, j+1 } }