-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscramble-string.go
More file actions
122 lines (112 loc) · 1.95 KB
/
scramble-string.go
File metadata and controls
122 lines (112 loc) · 1.95 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
)
// source: https://leetcode.com/problems/scramble-string/
func isScramble(s1 string, s2 string) bool {
if len(s1) != len(s1) {
return false
}
var s1Letters, s2Letters [26]int
for _, l := range s1 {
s1Letters[int(l-'a')]++
}
for _, l := range s2 {
s2Letters[int(l-'a')]++
}
if s1Letters != s2Letters {
return false
}
memo := make(map[string]bool)
var test func(s, t string) bool
test = func(s, t string) bool {
if s == t || len(s) == 1 {
return true
}
n := len(s)
var sl1, sl2, tl [26]int
key := s + t
if val, ok := memo[key]; ok {
return val
}
for i := 1; i < n; i++ {
sl1[s[i-1]-'a']++
sl2[s[n-i]-'a']++
tl[t[i-1]-'a']++
if sl1 == tl && test(s[:i], t[:i]) && test(s[i:], t[i:]) {
memo[key] = true
return true
}
if sl2 == tl && test(s[n-i:], t[:i]) && test(s[:n-i], t[i:]) {
memo[key] = true
return true
}
memo[key] = false
}
return false
}
return test(s1, s2)
}
func main() {
testCases := []struct {
s1 string
s2 string
want bool
}{
{
s1: "abc",
s2: "bca",
want: true,
},
{
s1: "abb",
s2: "bba",
want: true,
},
{
s1: "abcdbdacbdac",
s2: "bdacabcdbdac",
want: true,
},
{
s1: "a",
s2: "b",
want: false,
},
{
s1: "a",
s2: "a",
want: true,
},
{
s1: "great",
s2: "rgeat",
want: true,
},
{
s1: "abcde",
s2: "caebd",
want: false,
},
{
s1: "a",
s2: "a",
want: true,
},
}
successes := 0
for _, tc := range testCases {
x := isScramble(tc.s1, tc.s2)
status := "ERROR"
if fmt.Sprint(x) == fmt.Sprint(tc.want) {
status = "OK"
successes++
}
fmt.Println(status, " Expected: ", tc.want, " Actual: ", x)
}
if l := len(testCases); successes == len(testCases) {
fmt.Printf("===\nSUCCESS: %d of %d tests ended successfully\n", successes, l)
} else {
fmt.Printf("===\nFAIL: %d tests failed\n", l-successes)
}
}