-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path777.cpp
More file actions
76 lines (70 loc) · 2.15 KB
/
777.cpp
File metadata and controls
76 lines (70 loc) · 2.15 KB
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
__________________________________________________________________________________________________
sample 4 ms submission
static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
class Solution {
public:
bool canTransform(string start, string end) {
int i = 0;
int j = 0;
const int n = start.size();
while (i < n || j < n) {
i = getPos(start, i);
j = getPos(end, j);
if (i == n || j == n) {
return i == j;
}
if (start[i] != end[j]) {
return false;
}
if (start[i] == 'R') {
if (i > j) {
return false;
}
} else if (i < j) {
return false;
}
i++;
j++;
}
return true;
}
private:
int getPos(const string& str, int idx) {
while (idx < str.size() && str[idx] == 'X') {
idx++;
}
return idx;
}
};
__________________________________________________________________________________________________
sample 9468 kb submission
static int x = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool canTransform(string start, string end) {
if (start.length() != end.length()) return false;
int l = start.length();
int i = 0, j = 0;
while (i < l && j < l) {
while (i < l && start[i] == 'X') i++;
while (j < l && end[j] == 'X') j++;
if (i == l && j == l) return true;
if (i == l || j == l) return false;
if (start[i] != end[j]) return false;
if (start[i] == 'R') {
// R can be taken to the right
if (i > j) return false;
} else {
// L can be taken to the left
if (i < j) return false;
}
i++; j++;
}
return true;
}
};
__________________________________________________________________________________________________