# 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); } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up