Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit afd72a6

Browse files
authored
feat: add golang solution to lc problem: No.0087 (doocs#959)
No.0087.Scramble String
1 parent 4250319 commit afd72a6

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

‎solution/0000-0099/0087.Scramble String/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,43 @@ class Solution {
149149
}
150150
```
151151

152+
### **Go**
153+
154+
```go
155+
func isScramble(s1 string, s2 string) bool {
156+
n := len(s1)
157+
dp := make([][][]bool, n+1)
158+
for i := range dp {
159+
dp[i] = make([][]bool, n)
160+
for j := range dp[i] {
161+
dp[i][j] = make([]bool, n+1)
162+
}
163+
}
164+
for i := 0; i < n; i++ {
165+
for j := 0; j < n; j++ {
166+
dp[i][j][1] = s1[i] == s2[j]
167+
}
168+
}
169+
for l := 2; l < n+1; l++ {
170+
for i1 := 0; i1 < n-l+1; i1++ {
171+
for i2 := 0; i2 < n-l+1; i2++ {
172+
for i := 1; i < l; i++ {
173+
if dp[i1][i2][i] && dp[i1+i][i2+i][l-i] {
174+
dp[i1][i2][l] = true
175+
break
176+
}
177+
if dp[i1][i2+l-i][i] && dp[i1+i][i2][l-i] {
178+
dp[i1][i2][l] = true
179+
break
180+
}
181+
}
182+
}
183+
}
184+
}
185+
return dp[0][0][n]
186+
}
187+
```
188+
152189
### **...**
153190

154191
```

‎solution/0000-0099/0087.Scramble String/README_EN.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,43 @@ class Solution {
119119
}
120120
```
121121

122+
### **Go**
123+
124+
```go
125+
func isScramble(s1 string, s2 string) bool {
126+
n := len(s1)
127+
dp := make([][][]bool, n+1)
128+
for i := range dp {
129+
dp[i] = make([][]bool, n)
130+
for j := range dp[i] {
131+
dp[i][j] = make([]bool, n+1)
132+
}
133+
}
134+
for i := 0; i < n; i++ {
135+
for j := 0; j < n; j++ {
136+
dp[i][j][1] = s1[i] == s2[j]
137+
}
138+
}
139+
for l := 2; l < n+1; l++ {
140+
for i1 := 0; i1 < n-l+1; i1++ {
141+
for i2 := 0; i2 < n-l+1; i2++ {
142+
for i := 1; i < l; i++ {
143+
if dp[i1][i2][i] && dp[i1+i][i2+i][l-i] {
144+
dp[i1][i2][l] = true
145+
break
146+
}
147+
if dp[i1][i2+l-i][i] && dp[i1+i][i2][l-i] {
148+
dp[i1][i2][l] = true
149+
break
150+
}
151+
}
152+
}
153+
}
154+
}
155+
return dp[0][0][n]
156+
}
157+
```
158+
122159
### **...**
123160

124161
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func isScramble(s1 string, s2 string) bool {
2+
n := len(s1)
3+
dp := make([][][]bool, n+1)
4+
for i := range dp {
5+
dp[i] = make([][]bool, n)
6+
for j := range dp[i] {
7+
dp[i][j] = make([]bool, n+1)
8+
}
9+
}
10+
for i := 0; i < n; i++ {
11+
for j := 0; j < n; j++ {
12+
dp[i][j][1] = s1[i] == s2[j]
13+
}
14+
}
15+
for l := 2; l < n+1; l++ {
16+
for i1 := 0; i1 < n-l+1; i1++ {
17+
for i2 := 0; i2 < n-l+1; i2++ {
18+
for i := 1; i < l; i++ {
19+
if dp[i1][i2][i] && dp[i1+i][i2+i][l-i] {
20+
dp[i1][i2][l] = true
21+
break
22+
}
23+
if dp[i1][i2+l-i][i] && dp[i1+i][i2][l-i] {
24+
dp[i1][i2][l] = true
25+
break
26+
}
27+
}
28+
}
29+
}
30+
}
31+
return dp[0][0][n]
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /