Skip to content

Commit 6fffa99

Browse files
committed
Create 0097-interleaving-string.go
1 parent c1f03e5 commit 6fffa99

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

go/0097-interleaving-string.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var cache map[[2]int]bool
2+
var st1 string
3+
var st2 string
4+
var st3 string
5+
6+
func isInterleave(s1 string, s2 string, s3 string) bool {
7+
if len(s1)+len(s2) != len(s3) {
8+
return false
9+
}
10+
cache = make(map[[2]int]bool)
11+
st1 = s1
12+
st2 = s2
13+
st3 = s3
14+
return dfs(0, 0)
15+
}
16+
17+
func dfs(i, j int) bool {
18+
if i >= len(st1) && j >= len(st2) {
19+
return true
20+
}
21+
22+
val, ok := cache[[2]int{i, j}]
23+
24+
if ok {
25+
return val
26+
}
27+
28+
if i < len(st1) && st1[i] == st3[i+j] && dfs(i+1, j) {
29+
return true
30+
}
31+
if j < len(st2) && st2[j] == st3[i+j] && dfs(i, j+1) {
32+
return true
33+
}
34+
35+
cache[[2]int{i, j}] = false
36+
return false
37+
}

0 commit comments

Comments
 (0)