---
# System prepended metadata

title: 1657. Determine if Two Strings Are Close
tags: [Leetcode, HashMap, Medium]

---

# 1657. Determine if Two Strings Are Close
###### tags: `Leetcode` `Medium` `HashMap`
Link: https://leetcode.com/problems/determine-if-two-strings-are-close/
## 思路
检查长度
检查出现过的字母
检查排序过后的freq一不一样
## Code
```java=
class Solution {
    public boolean closeStrings(String word1, String word2) {
        if(word1.length()!=word2.length()) return false;
        int[] freq1 = new int[26];
        int[] freq2 = new int[26];
        Set<Character> c1 = new HashSet<>();
        Set<Character> c2 = new HashSet<>();
        
        for(int i=0; i<word1.length(); i++){
            freq1[word1.charAt(i)-'a']++;
            freq2[word2.charAt(i)-'a']++;
            c1.add(word1.charAt(i));
            c2.add(word2.charAt(i));
        }
        if(!c1.equals(c2)) return false;
        Arrays.sort(freq1);
        Arrays.sort(freq2);
        return Arrays.equals(freq1, freq2);
    }
}
```