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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package com.demo.s87;
import java.util.HashMap; import java.util.Map;
public class Solution { int[][][] memo; String s1, s2;
public boolean isScramble(String s1, String s2) { int length = s1.length(); this.memo = new int[length][length][length + 1]; this.s1 = s1; this.s2 = s2; return dfs(0, 0, length); }
public boolean dfs(int i1, int i2, int length) { if (memo[i1][i2][length] != 0) { return memo[i1][i2][length] == 1; }
if (s1.substring(i1, i1 + length).equals(s2.substring(i2, i2 + length))) { memo[i1][i2][length] = 1; return true; }
if (!checkIfSimilar(i1, i2, length)) { memo[i1][i2][length] = -1; return false; }
for (int i = 1; i < length; ++i) { if (dfs(i1, i2, i) && dfs(i1 + i, i2 + i, length - i)) { memo[i1][i2][length] = 1; return true; } if (dfs(i1, i2 + length - i, i) && dfs(i1 + i, i2, length - i)) { memo[i1][i2][length] = 1; return true; } }
memo[i1][i2][length] = -1; return false; }
public boolean checkIfSimilar(int i1, int i2, int length) { Map<Character, Integer> freq = new HashMap<Character, Integer>(); for (int i = i1; i < i1 + length; ++i) { char c = s1.charAt(i); freq.put(c, freq.getOrDefault(c, 0) + 1); } for (int i = i2; i < i2 + length; ++i) { char c = s2.charAt(i); freq.put(c, freq.getOrDefault(c, 0) - 1); } for (Map.Entry<Character, Integer> entry : freq.entrySet()) { int value = entry.getValue(); if (value != 0) { return false; } } return true; } }
|