2337. 移动片段得到字符串
为保证权益,题目请参考 2337. 移动片段得到字符串(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <string>
using namespace std;
class Solution
{
public:
bool canChange(string start, string target)
{
if (start.length() != target.size())
{
return false;
}
{
int startIdx = 0;
int targetIdx = 0;
while (startIdx < start.size() && targetIdx < target.size())
{
while (startIdx < start.size() && start[startIdx] == '_')
{
startIdx += 1;
}
while (targetIdx < target.size() && target[targetIdx] == '_')
{
targetIdx += 1;
}
if ((start[startIdx] != target[targetIdx]) || (start[startIdx] == 'L' && startIdx < targetIdx) ||
(start[startIdx] == 'R' && startIdx > targetIdx))
{
return false;
}
startIdx += 1;
targetIdx += 1;
}
while (startIdx < start.size() && start[startIdx] == '_')
{
startIdx += 1;
}
while (targetIdx < target.size() && target[targetIdx] == '_')
{
targetIdx += 1;
}
if (startIdx != targetIdx)
{
return false;
}
}
return true;
}
};
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
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