-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path777. Swap Adjacent in LR String.c
58 lines (45 loc) · 1.56 KB
/
777. Swap Adjacent in LR String.c
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
/*
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.
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'}.
*/
// L can move ahead of X, R can move after X.
// skip X from start and end:
// if both are L, and this L in start does not appear earlier than the L in end, this is ok since L can move ahead;
// if both are R, and this R in start does not appear later than the R in end, this is ok since R can move afterwards.
bool canTransform(char * start, char * end){
int i, j;
i = j = 0;
while (start[i] && end[i]) {
while (start[i] == 'X') i ++;
while (end[j] == 'X') j ++;
if (start[i] == 0 && end[j] == 0) return true;
if (start[i] == 0 || end[j] == 0) return false;
if (start[i] != end[j] ||
(start[i] == 'L' && i < j) ||
(start[i] == 'R' && i > j)) return false;
i ++; j ++;
}
return true;
}
/*
Difficulty:Medium
*/