0%

Leetcode777-Swap Adjacent In LR String

Description

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

Example 1:

1
2
3
4
5
6
7
8
Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
Output: true
Explanation: We can transform start to end following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX

Example 2:
1
2
Input: start = "X", end = "L"
Output: false

Example 3:
1
2
Input: start = "LLR", end = "RRL"
Output: false

Example 4:
1
2
Input: start = "XL", end = "LX"
Output: true

Example 5:
1
2
Input: start = "XLLR", end = "LXLX"
Output: false

Constraints:

  • 1 <= start.length <= 104
  • start.length == end.length
  • Both start and end will only consist of characters in ‘L’, ‘R’, and ‘X’.

Solution

  • Time Complexity:
  • Space Complexity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public boolean canTransform(String start, String end) {
if (start.length() != end.length()) return false;
// if (!start.replace("X", "").equals(end.replace("X", ""))) return false;
int i = 0;
int j = 0;
char[] startArr = start.toCharArray();
char[] endArr = end.toCharArray();
// while (i < startArr.length && j < endArr.length){
while (i < startArr.length || j < endArr.length){
while(i < startArr.length && startArr[i] == 'X') i++;
while(j < endArr.length && endArr[j] == 'X') j++;

if (i == startArr.length && j == endArr.length) return true;
if (i == startArr.length || j == endArr.length) return false;

if (startArr[i] != endArr[j]) return false;
if (startArr[i] == 'L' && i < j) return false;
if (startArr[i] == 'R' && i > j) return false;
i++;
j++;
}
// return true;
return i == j;
}
}