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
| class Solution { int ans = Integer.MAX_VALUE; char[] cs1, cs2;
public int kSimilarity(String s1, String s2) { cs1 =s1.toCharArray(); cs2 = s2.toCharArray(); return dfs(0, 0); }
private int dfs(int start, int cur) { if (cur >= ans) return ans; if (start == cs1.length - 1) return ans = cur;
for (int i = start; i < cs1.length; ++i) { if (cs1[i] != cs2[i]) { for (int j = i + 1; j < cs2.length; ++j) { if (cs2[j] == cs1[i] && cs2[j] != cs1[j]) { swap(cs2, i, j); dfs(i + 1, cur + 1); swap(cs2, i, j); if (cs2[i] == cs1[j]) break; } } return ans; } } return ans = cur; }
private void swap(char[] cs, int i, int j){ char temp = cs[i]; cs[i] = cs[j]; cs[j] = temp; } }
|