Skip to content

Commit 60bcb46

Browse files
refactor 87
1 parent 40ec471 commit 60bcb46

File tree

1 file changed

+28
-66
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,42 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 87. Scramble String
5-
*
6-
* Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
7-
8-
Below is one possible representation of s1 = "great":
9-
10-
great
11-
/ \
12-
gr eat
13-
/ \ / \
14-
g r e at
15-
/ \
16-
a t
17-
To scramble the string, we may choose any non-leaf node and swap its two children.
18-
19-
For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
20-
21-
rgeat
22-
/ \
23-
rg eat
24-
/ \ / \
25-
r g e at
26-
/ \
27-
a t
28-
We say that "rgeat" is a scrambled string of "great".
29-
30-
Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
31-
32-
rgtae
33-
/ \
34-
rg tae
35-
/ \ / \
36-
r g ta e
37-
/ \
38-
t a
39-
We say that "rgtae" is a scrambled string of "great".
40-
41-
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
42-
*/
433
public class _87 {
444

455
public static class Solution1 {
46-
/** credit: https://discuss.leetcode.com/topic/19158/accepted-java-solution */
6+
/**
7+
* credit: https://discuss.leetcode.com/topic/19158/accepted-java-solution
8+
*/
479
public boolean isScramble(String s1, String s2) {
48-
if (s1.equals(s2)) {
49-
return true;
50-
}
51-
if (s1.length() != s2.length()) {
52-
return false;
53-
}
54-
55-
int[] letters = new int[26];
56-
for (int i = 0; i < s1.length(); i++) {
57-
letters[s1.charAt(i) - 'a']++;
58-
letters[s2.charAt(i) - 'a']--;
59-
}
10+
if (s1.equals(s2)) {
11+
return true;
12+
}
13+
if (s1.length() != s2.length()) {
14+
return false;
15+
}
6016

61-
for (int i : letters) {
62-
if (i != 0) {
63-
return false;
17+
int[] letters = new int[26];
18+
for (int i = 0; i < s1.length(); i++) {
19+
letters[s1.charAt(i) - 'a']++;
20+
letters[s2.charAt(i) - 'a']--;
6421
}
65-
}
6622

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;
23+
for (int i : letters) {
24+
if (i != 0) {
25+
return false;
26+
}
7127
}
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;
28+
29+
for (int i = 1; i < s1.length(); i++) {
30+
if (isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(
31+
s1.substring(i), s2.substring(i))) {
32+
return true;
33+
}
34+
if (isScramble(s1.substring(0, i), s2.substring(s2.length() - i)) && isScramble(
35+
s1.substring(i), s2.substring(0, s2.length() - i))) {
36+
return true;
37+
}
7538
}
76-
}
77-
return false;
39+
return false;
7840
}
7941
}
8042
}

0 commit comments

Comments
 (0)