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
| class Solution { public boolean closeStrings(String word1, String word2) { if (word1.length() != word2.length()) { return false; }
int[] countMapA = new int[26]; int[] countMapB = new int[26];
for (int i = 0; i < word1.length(); i++) { countMapA[word1.charAt(i) - 'a']++; countMapB[word2.charAt(i) - 'a']++; }
for (int i = 0; i < countMapA.length; i++) { if (countMapA[i] == 0 && countMapB[i] == 0) { continue; } if (countMapA[i] == 0 || countMapB[i] == 0) { return false; } }
Arrays.sort(countMapA); Arrays.sort(countMapB);
return Arrays.equals(countMapA, countMapB); } }
|
References
1657. Determine if Two Strings Are Close