-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
42 lines (41 loc) · 1.02 KB
/
Copy pathSolution.java
File metadata and controls
42 lines (41 loc) · 1.02 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
public class Solution
{
public boolean isScramble(String s1, String s2)
{
if (s1.equals(s2))
{
return true;
}
if (s1.length() != s2.length())
{
return false;
}
int[] count = new int[26];
int n = s1.length();
for (int i = 0; i < n; i++)
{
count[s1.charAt(i) - 'a']++;
count[s2.charAt(i) - 'a']--;
}
for (int i = 0; i < 26; i++)
{
if (count[i] != 0)
{
return false;
}
}
for (int i = 1; i < n; i++)
{
if (isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(s1.substring(i), s2.substring(i)))
{
return true;
}
if (isScramble(s1.substring(0, i), s2.substring(n - i)) && isScramble(s1.substring(i), s2.substring(0,
n - i)))
{
return true;
}
}
return false;
}
}