-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution87.java
More file actions
60 lines (52 loc) · 2.01 KB
/
Solution87.java
File metadata and controls
60 lines (52 loc) · 2.01 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
import java.util.HashMap;
import java.util.Map;
public class Solution87 {
private static Map<String, Map<String, Boolean>> memory = new HashMap<>();
public static boolean isScramble(String s1, String s2) {
int n = s1.length();
if (s1.equals(s2)) return true;
else if (n == 1) return false;
// 简单递归超时
if (memory.containsKey(s1) && memory.get(s1).containsKey(s2))
return memory.get(s1).get(s2);
// check 字符组成
Map<Character, Integer> map1 = new HashMap<>();
for (char c : s1.toCharArray()) {
int i = map1.getOrDefault(c, 0);
map1.put(c, i + 1);
}
for (char c : s2.toCharArray()) {
if (!map1.containsKey(c)) return false;
if (map1.get(c) == 0) return false;
map1.put(c, map1.get(c) - 1);
}
// 没有判断s1.s2的长度和组成
Map<String, Boolean> tmp;
for (int i = 1; i < n; ++i) {
boolean a = isScramble(s1.substring(0, i), s2.substring(n - i, n));
boolean b = isScramble(s2.substring(0, n - i), s1.substring(i, n));
if (a && b) {
tmp = memory.getOrDefault(s1, new HashMap<>());
tmp.put(s2, true);
memory.put(s1, tmp);
return true;
}
a = isScramble(s1.substring(0, i), s2.substring(0, i));
b = isScramble(s2.substring(i, n), s1.substring(i, n));
if (a && b) {
tmp = memory.getOrDefault(s1, new HashMap<>());
tmp.put(s2, true);
memory.put(s1, tmp);
return true;
}
}
tmp = memory.getOrDefault(s1, new HashMap<>());
tmp.put(s2, false);
memory.put(s1, tmp);
return false;
}
public static void main(String[] args) {
String s = "great", t = "rgeat";
System.out.println(isScramble(s,t));
}
}