Two strings are considered close if you can attain one from the other using the following operations:


	Operation 1: Swap any two existing characters.

	
		For example, abcde -&gt; aecdb
	
	
	Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
	
		For example, aacabb -&gt; bbcbaa (all a's turn into b's, and all b's turn into a's)
	
	


You can use the operations on either string as many times as necessary.

Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.

&nbsp;
Example 1:

Input: word1 = "abc", word2 = "bca"
Output: true
Explanation: You can attain word2 from word1 in 2 operations.
Apply Operation 1: "abc" -&gt; "acb"
Apply Operation 1: "acb" -&gt; "bca"


Example 2:

Input: word1 = "a", word2 = "aa"
Output: false
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.


Example 3:

Input: word1 = "cabbba", word2 = "abbccc"
Output: true
Explanation: You can attain word2 from word1 in 3 operations.
Apply Operation 1: "cabbba" -&gt; "caabbb"
Apply Operation 2: "caabbb" -&gt; "baaccc"
Apply Operation 2: "baaccc" -&gt; "abbccc"


&nbsp;
Constraints:


	1 &lt;= word1.length, word2.length &lt;= 105
	word1 and word2 contain&nbsp;only lowercase English letters.


from collections import Counter

class Solution:
    def closeStrings(self, word1: str, word2: str) -> bool:
        return set(word1) == set(word2) and Counter(Counter(word1).values()) == Counter(Counter(word2).values())
class Solution {
    private int N = 26;
    public boolean closeStrings(String word1, String word2) {
        // count the English letters
        int[] arr1 = new int[N], arr2 = new int[N];
        for (char ch : word1.toCharArray())
            arr1[ch - 'a']++;
        for (char ch : word2.toCharArray())
            arr2[ch - 'a']++;

        // if one has a letter which another one doesn't have, dont exist
        for (int i = 0; i < N; i++) {
            if (arr1[i] == arr2[i]) {
                continue;
            }
            if (arr1[i] == 0 || arr2[i] == 0) {
                return false;
            }
        }
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        for (int i = 0; i < N; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }
}
class Solution {
public:
    bool closeStrings(string word1, string word2) {
        set<int> w1_letters, w2_letters, w1_freq, w2_freq;
        unordered_map<char, int> w1_m, w2_m;
        
        for (auto a : word1) {
            w1_letters.insert(a);
            w1_m[a]++;
        }
        
        for (auto a : word2) {
            w2_letters.insert(a);
            w2_m[a]++;
        }
        
        for (auto [k, v] : w1_m) w1_freq.insert(v);
        
        for (auto [k, v] : w2_m) w2_freq.insert(v);

        return w1_letters == w2_letters && w1_freq == w2_freq;
    }
};
var closeStrings = function(word1, word2) {
    if (word1.length != word2.length) return false;
    let map1 = new Map(), map2 = new Map(),freq1=new Set(),freq2=new Set();
    for (let i = 0; i < word1.length; i++) map1.set(word1[i],map1.get(word1[i])+1||1),map2.set(word2[i],map2.get(word2[i])+1||1);
    if (map1.size != map2.size) return false;
    for (const [key,value] of map1) {
      if (!map2.has(key)) return false
      freq1.add(value);
      freq2.add(map2.get(key));
    }
    if (freq1.size != freq2.size) return false;
    for (const freq of freq1) if (!freq2.has(freq)) return false
    return true;    
};
Determine if Two Strings Are Close