|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
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 |
| - */ |
43 | 3 | public class _87 {
|
44 | 4 |
|
45 | 5 | 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 | + */ |
47 | 9 | 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 | + } |
60 | 16 |
|
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']--; |
64 | 21 | }
|
65 |
| - } |
66 | 22 |
|
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 | + } |
71 | 27 | }
|
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 | + } |
75 | 38 | }
|
76 |
| - } |
77 |
| - return false; |
| 39 | + return false; |
78 | 40 | }
|
79 | 41 | }
|
80 | 42 | }
|
0 commit comments