forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.cpp
45 lines (45 loc) · 1.33 KB
/
s1.cpp
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
class Solution {
private:
unordered_map<char, int> m;
string board;
void clean(string &board) {
bool found = true;
while (found && !board.empty()) {
found = false;
int start, i = 0;
while (i < board.size() && !found) {
start = i;
do { ++i; } while (i < board.size() && board[i] == board[i - 1]);
if (i - start >= 3) found = true;
}
if (found) {
board.erase(start, i - start);
}
}
}
int dfs(string board) {
if (board.empty()) return 0;
int minStep = INT_MAX;
for (int i = 0; i < board.size();) {
do { ++i; } while (i < board.size() && board[i] == board[i - 1]);
char c = board[i - 1];
if (m[c]) {
--m[c];
string newBoard(board);
newBoard.insert(i, 1, c);
clean(newBoard);
int ans = dfs(newBoard);
if (ans != INT_MAX) minStep = min(minStep, 1 + ans);
++m[c];
}
}
return minStep;
}
public:
int findMinStep(string board, string hand) {
this->board = board;
for (char c : hand) m[c]++;
int ans = dfs(board);
return ans == INT_MAX ? -1 : ans;
}
};