-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindMinStep.cpp
More file actions
30 lines (30 loc) · 1.09 KB
/
findMinStep.cpp
File metadata and controls
30 lines (30 loc) · 1.09 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
class Solution {
public:
int findMinStep(string board, string hand) {
int res = INT_MAX;
unordered_set<char> s;
for (int i = 0; i < hand.size(); ++i) {
if (s.count(hand[i])) continue;
s.insert(hand[i]);
for (int j = 0; j < board.size(); ++j) {
if (board[j] != hand[i]) continue;
string newBoard = board, newHand = hand;
newBoard.insert(j, 1, hand[i]);
newBoard = removeConsecutive(newBoard);
if (newBoard.size() == 0) return 1;
newHand.erase(i, 1);
int cnt = findMinStep(newBoard, newHand);
if (cnt != -1) res = min(res, cnt + 1);
}
}
return res == INT_MAX ? -1 : res;
}
string removeConsecutive(string board) {
for (int i = 0, j = 0; i <= board.size(); ++i) {
if (i < board.size() && board[i] == board[j]) continue;
if (i - j >= 3) return removeConsecutive(board.substr(0, j) + board.substr(i));
else j = i;
}
return board;
}
};