-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0087.Scramble String.swift
43 lines (35 loc) · 1.49 KB
/
0087.Scramble String.swift
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
class Solution {
func isScramble(_ s1: String, _ s2: String) -> Bool {
let chars1 = Array(s1)
let chars2 = Array(s2)
let n = chars1.count
var dp = Array(
repeating: Array(repeating: Array(repeating: false, count: n + 1), count: n),
count: n
)
for substrLen in 1...n {
var strStartIdx1 = 0
while (strStartIdx1 + substrLen) <= n {
var strStartIdx2 = 0
while (strStartIdx2 + substrLen) <= n {
if substrLen == 1 {
dp[strStartIdx1][strStartIdx2][substrLen] = chars1[strStartIdx1] == chars2[strStartIdx2]
} else {
for splitIdx in 1..<substrLen {
if dp[strStartIdx1][strStartIdx2][substrLen] {
continue
}
dp[strStartIdx1][strStartIdx2][substrLen] =
(dp[strStartIdx1][strStartIdx2][splitIdx] && dp[strStartIdx1 + splitIdx][strStartIdx2 + splitIdx][substrLen - splitIdx])
||
(dp[strStartIdx1][strStartIdx2 + substrLen - splitIdx][splitIdx] && dp[strStartIdx1 + splitIdx][strStartIdx2][substrLen - splitIdx])
}
}
strStartIdx2 += 1
}
strStartIdx1 += 1
}
}
return dp[0][0][n]
}
}