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 3d33c4f

Browse files
87. Scramble String (java)
1 parent d677146 commit 3d33c4f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean isScramble(String s1, String s2) {
3+
if(s1.equals(s2)) return true;
4+
if(s1.length()!=s2.length()) return false;
5+
int len = s1.length();
6+
int[] count = new int[26];
7+
for(int i = 0; i < len; i++){
8+
count[s1.charAt(i) - 'a']++;
9+
count[s2.charAt(i) - 'a']--;
10+
}
11+
for(int item : count) if (item != 0) return false;
12+
for(int i = 1; i <= len - 1; i++){
13+
if(isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(s1.substring(i), s2.substring(i)))
14+
return true;
15+
if (isScramble(s1.substring(0, i), s2.substring(len - i)) &&
16+
isScramble(s1.substring(i), s2.substring(0, len - i)))
17+
return true;
18+
}
19+
return false;
20+
}
21+
}

0 commit comments

Comments
(0)

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