[LeetCode] Swap Adjacent in LR String

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 one string to the other.

  • new solution update 2022.06.02
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
27
28
29
class Solution {
public:
bool canTransform(string start, string end) {
int n = start.length();
for(int i = 0; i < n; i++) {
if(start[i] == end[i]) continue;
if(i == n - 1) return false;
if(start[i] == 'L') return false;
if(end[i] == 'L') {
if(start[i] == 'R') return false;
int j = i + 1;
for(; j < n and start[j] != 'L'; j++) {
if(start[j] == 'R') return false;
}
if(j == n or start[j] == 'R') return false;
swap(start[j], start[i]);
} else if(start[i] == 'R') {
int j = i + 1;
for(; j < n and start[j] == 'R'; j++) {
if(start[j] == 'L') return false;
}
if(j == n) return false;
swap(start[i], start[j]);
}
if(start[i] != end[i]) return false;
}
return true;
}
};
  • new solution update 2022.02.04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
bool canSwap(char a, char b) {
if(a == 'X') return b == 'L';
else return a == 'R' and b == 'X';
}
public:
bool canTransform(string start, string end) {
for(int i = 0; i < start.length(); i++) {
if(start[i] == end[i]) continue;
auto pos = start.find(end[i], i);
if(pos == string::npos) return false;
for(int j = pos; j > i; j--) {
if(canSwap(start[j-1], start[j])) {
swap(start[j], start[j-1]);
} else return false;
}

}
return true;
}
};
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
bool isContainLetters(string& start, string& end) {
if(start.length() != end.length())
return false;
map<char, int> m;
int sz = start.length();
for(int i = 0; i < sz; i++) {
m[start[i]]++;
m[end[i]]--;
}

for(auto& letter : m) {
if(letter.second)
return false;
}
return true;
}
string getNextStart(string start, int target, int cur) {
char status = start[cur];
while(target != cur) {
if(status == 'L') {
if(start[cur - 1] == 'X')
swap(start[cur], start[cur - 1]);
else
return "";
}
else if(status == 'X') {
if(start[cur - 1] == 'R') {
swap(start[cur], start[cur - 1]);
}
else
return "";
}
cur--;
}
return start;
}
bool dfs(int pos, string start, string end) {
if(start == "") return false;
if(pos == start.length()) return true;
if(start[pos] == end[pos]) return dfs(pos + 1, start, end);
for(int i = pos; i < start.length(); i++) {
if(start[i] == end[pos]) return dfs(pos + 1, getNextStart(start, pos, i), end);
}
return false;
}
public:
bool canTransform(string start, string end) {
if(!isContainLetters(start, end)) {
return false;
}

return dfs(0, start, end);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/04/10/PS/LeetCode/swap-adjacent-in-lr-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.