1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public boolean closeStrings(String word1, String word2) { int n = word1.length(), m = word2.length(); if (n != m) return false; int[] cnt1 = new int[26], cnt2 = new int[26]; for (int i = 0; i < n; ++i) { ++cnt1[word1.charAt(i) - 'a']; ++cnt2[word2.charAt(i) - 'a']; } for (int i = 0; i < 26; ++i) { if ((cnt1[i] == 0) != (cnt2[i] == 0)) { return false; } } Arrays.sort(cnt1); Arrays.sort(cnt2); return Arrays.equals(cnt1, cnt2); } }
|