-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswap_adjacent_lr_string.cpp
More file actions
74 lines (69 loc) · 1.66 KB
/
swap_adjacent_lr_string.cpp
File metadata and controls
74 lines (69 loc) · 1.66 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
/*
* =====================================================================================
*
* Filename: swap_adjacent_lr_string.cpp
*
* Description: 777. Swap Adjacent in LR String
*
* Version: 1.0
* Created: 11/08/2025 16:24:31
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <string>
#include <tuple>
#include <vector>
#include "gtest/gtest.h"
class Solution {
public:
bool canTransform(std::string start, std::string result) {
const int n = start.length();
if (n != result.length()) {
return false;
}
int i = 0;
int j = 0;
while (i < n || j < n) {
while (i < n && start[i] == 'X') {
i++;
}
while (j < n && result[j] == 'X') {
j++;
}
if (i == n && j == n) {
return true;
}
if (i == n || j == n) {
return false;
}
if (start[i] != result[j]) {
return false;
}
if (start[i] == 'L' && i < j) {
return false;
}
if (start[i] == 'R' && i > j) {
return false;
}
i++;
j++;
}
return (i == n && j == n);
}
};
TEST(Solution, canTransform) {
std::vector<std::tuple<std::string, std::string, bool>> cases = {
{"RXXLRXRXL", "XRLXXRRLX", true},
{"X", "L", false},
{"LXXLXRLXXL", "XLLXRXLXLX", false},
{"XXXRXXLXXXXXXXXRXXXR", "XXXXRLXXXXXXXXXXXXRR", true},
};
for (auto& [start, result, existed] : cases) {
EXPECT_EQ(Solution().canTransform(start, result), existed);
}
}