forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
21 lines (21 loc) · 675 Bytes
/
s2.cpp
File metadata and controls
21 lines (21 loc) · 675 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// OJ: https://leetcode.com/problems/swap-adjacent-in-lr-string/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
bool canTransform(string start, string end) {
int i = 0, j = 0, N = start.size();
for (; i < N && j < N; ++i, ++j) {
while (i < N && start[i] == 'X') ++i;
while (j < N && end[j] == 'X') ++j;
if ((i < N) ^ (j < N)) return false;
if (i < N && j < N) {
if (start[i] != end[j]
|| (start[i] == 'L' && i < j)
|| (start[i] == 'R' && i > j)) return false;
}
}
return true;
}
};