We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c1f03e5 commit 6fffa99Copy full SHA for 6fffa99
go/0097-interleaving-string.go
@@ -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
30
31
+ if j < len(st2) && st2[j] == st3[i+j] && dfs(i, j+1) {
32
33
34
35
+ cache[[2]int{i, j}] = false
36
37
0 commit comments