-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path777.swap-adjacent-in-lr-string.cpp
83 lines (79 loc) · 2.16 KB
/
777.swap-adjacent-in-lr-string.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* @lc app=leetcode id=777 lang=cpp
*
* [777] Swap Adjacent in LR String
*
* https://leetcode.com/problems/swap-adjacent-in-lr-string/description/
*
* algorithms
* Medium (33.48%)
* Total Accepted: 15.8K
* Total Submissions: 47.1K
* Testcase Example: '"X"\n"L"'
*
* 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'}.
*
*
*/
class Solution {
public:
bool canTransform(string start, string end) {
int n = start.size();
if(start.size() != end.size())
return false;
if(start == end) return true;
int i = 0;
while(i<n){
if(start[i] == end[i]){
i++;
continue;
}
if(start[i]=='X' && end[i] == 'L'){
int sl = 0, el = 1;
i++;
while(i<n && el>sl && start[i] != 'R' && end[i] != 'R'){
sl += start[i] == 'L';
el += end[i] == 'L';
i++;
}
if(sl!=el) return false;
}else if(start[i] == 'R' && end[i] == 'X'){
int sr = 1, er = 0;
i++;
while(i<n && sr>er && start[i] != 'L' && end[i] != 'L'){
sr += start[i] == 'R';
er += end[i] == 'R';
i++;
}
if(sr!=er) return false;
}else{
return false;
}
}
return true;
}
};