Skip to content

Commit d3a0e5f

Browse files
authored
Create move-pieces-to-obtain-a-string.cpp
1 parent 6e9ec14 commit d3a0e5f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// two pointers
5+
class Solution {
6+
public:
7+
bool canChange(string start, string target) {
8+
for (int i = 0, j = 0; true; ++i, ++j) {
9+
for (; i < size(start) && start[i] == '_'; ++i);
10+
for (; j < size(target) && target[j] == '_'; ++j);
11+
if (i == size(start) && j == size(target)) {
12+
break;
13+
}
14+
if (i == size(start) || j == size(target) || start[i] != target[j] ||
15+
(start[i] == 'L' && i < j) || (start[i] == 'R' && i > j)) {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
21+
};

0 commit comments

Comments
 (0)