Skip to content

Commit 255faef

Browse files
committed
add: leetcode 0488 solution
1 parent df54373 commit 255faef

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetcode
2+
3+
func findMinStep(board string, hand string) int {
4+
q := [][]string{{board, hand}}
5+
mp := make(map[string]bool)
6+
minStep := 0
7+
for len(q) > 0 {
8+
length := len(q)
9+
minStep++
10+
for length > 0 {
11+
length--
12+
cur := q[0]
13+
q = q[1:]
14+
curB, curH := cur[0], cur[1]
15+
for i := 0; i < len(curB); i++ {
16+
for j := 0; j < len(curH); j++ {
17+
curB2 := del3(curB[0:i] + string(curH[j]) + curB[i:])
18+
curH2 := curH[0:j] + curH[j+1:]
19+
if len(curB2) == 0 {
20+
return minStep
21+
}
22+
if _, ok := mp[curB2+curH2]; ok {
23+
continue
24+
}
25+
mp[curB2+curH2] = true
26+
q = append(q, []string{curB2, curH2})
27+
}
28+
}
29+
}
30+
}
31+
return -1
32+
}
33+
34+
func del3(str string) string {
35+
cnt := 1
36+
for i := 1; i < len(str); i++ {
37+
if str[i] == str[i-1] {
38+
cnt++
39+
} else {
40+
if cnt >= 3 {
41+
return del3(str[0:i-cnt] + str[i:])
42+
}
43+
cnt = 1
44+
}
45+
}
46+
if cnt >= 3 {
47+
return str[0 : len(str)-cnt]
48+
}
49+
return str
50+
}

0 commit comments

Comments
 (0)