Skip to content

Commit 2d01f2f

Browse files
refactor 87
1 parent 1a03f8b commit 2d01f2f

File tree

3 files changed

+45
-44
lines changed

3 files changed

+45
-44
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ Your ideas/fixes/algorithms are more than welcome!
602602
|90|[Subsets II](https://leetcode.com/problems/subsets-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_90.java)|O(n^2) |O(1)||Medium|Backtracking
603603
|89|[Gray Code](https://leetcode.com/problems/gray-code/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_89.java)|O(n) |O(1)||Medium|Bit Manipulation
604604
|88|[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_88.java)|O(max(m,n)) |O(1)||Easy|
605-
|87|[Scramble String](https://leetcode.com/problems/scramble-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_87.java)|O(?) |O(?)||Hard| Recursion
605+
|87|[Scramble String](https://leetcode.com/problems/scramble-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_87.java)|O(n^4) |O(n^3||Hard| Recursion
606606
|86|[Partition List](https://leetcode.com/problems/partition-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_86.java)|O(n) |O(1)||Medium| Linked List
607607
|85|[Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_85.java)|O(m*n) |O(n)||Hard|DP
608608
|84|[Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_84.java)|O(n) |O(n)||Hard|Array, Stack

src/main/java/com/fishercoder/solutions/_87.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,39 @@
4242
*/
4343
public class _87 {
4444

45-
/**credit: https://discuss.leetcode.com/topic/19158/accepted-java-solution*/
46-
public boolean isScramble(String s1, String s2) {
47-
if (s1.equals(s2)) {
45+
public static class Solution1 {
46+
/** credit: https://discuss.leetcode.com/topic/19158/accepted-java-solution */
47+
public boolean isScramble(String s1, String s2) {
48+
if (s1.equals(s2)) {
4849
return true;
49-
}
50+
}
51+
if (s1.length() != s2.length()) {
52+
return false;
53+
}
5054

51-
int[] letters = new int[26];
52-
for (int i = 0; i < s1.length(); i++) {
55+
int[] letters = new int[26];
56+
for (int i = 0; i < s1.length(); i++) {
5357
letters[s1.charAt(i) - 'a']++;
5458
letters[s2.charAt(i) - 'a']--;
55-
}
59+
}
5660

57-
for (int i : letters) {
61+
for (int i : letters) {
5862
if (i != 0) {
59-
return false;
63+
return false;
6064
}
61-
}
65+
}
6266

63-
for (int i = 1; i < s1.length(); i++) {
64-
if (isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(s1.substring(i), s2.substring(i))) {
65-
return true;
67+
for (int i = 1; i < s1.length(); i++) {
68+
if (isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(
69+
s1.substring(i), s2.substring(i))) {
70+
return true;
6671
}
67-
if (isScramble(s1.substring(0, i), s2.substring(s2.length() - i)) && isScramble(s1.substring(i), s2.substring(0, s2.length() - i))) {
68-
return true;
72+
if (isScramble(s1.substring(0, i), s2.substring(s2.length() - i)) && isScramble(
73+
s1.substring(i), s2.substring(0, s2.length() - i))) {
74+
return true;
6975
}
76+
}
77+
return false;
7078
}
71-
72-
return false;
7379
}
74-
7580
}

src/test/java/com/fishercoder/_87Test.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,26 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/4/17.
11-
*/
129
public class _87Test {
13-
private static _87 test;
14-
15-
@BeforeClass
16-
public static void setup() {
17-
test = new _87();
18-
}
19-
20-
@Test
21-
public void test1() {
22-
assertEquals(true, test.isScramble("great", "rgeat"));
23-
}
24-
25-
@Test
26-
public void test2() {
27-
assertEquals(true, test.isScramble("great", "rgtae"));
28-
}
29-
30-
@Test
31-
public void test3() {
32-
assertEquals(true, test.isScramble("abc", "bca"));
33-
}
34-
10+
private static _87.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _87.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.isScramble("great", "rgeat"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.isScramble("great", "rgtae"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.isScramble("abc", "bca"));
30+
}
3531
}

0 commit comments

Comments
 (0)