1657. Determine if Two Strings Are Close

String closeness, in the context of this problem, involves evaluating whether two words can be considered “close” by meeting certain criteria. The criteria include having the same set of characters and the same frequency distribution of those characters. The closeStrings method endeavors to address this challenge.

class Solution {
    public boolean closeStrings(String word1, String word2) {
        boolean check = basicCheck(word1, word2);
        if (!check) {
            return false;
        }

        int freq[] = new int[26];
        char s1[] = word1.toCharArray();
        for(int i=0; i<s1.length; i++) {
            freq[s1[i]-'a']++;
        }

        HashMap<Integer, Integer> string1Frequency = new HashMap<Integer, Integer>();
        for(int i=0; i<freq.length; i++) {
            if (string1Frequency.containsKey(freq[i])) {
                string1Frequency.put(freq[i], string1Frequency.get(freq[i])+1);
            } else {
               string1Frequency.put(freq[i], 1); 
            }
        }

        freq = new int[26];
        s1 = word2.toCharArray();
        for(int i=0; i<s1.length; i++) {
            freq[s1[i]-'a']++;
        }

        HashMap<Integer, Integer> string2Frequency = new HashMap<Integer, Integer>();
        for(int i=0; i<freq.length; i++) {
            if (string2Frequency.containsKey(freq[i])) {
                string2Frequency.put(freq[i], string2Frequency.get(freq[i])+1);
            } else {
               string2Frequency.put(freq[i], 1); 
            }
        }

        for (Map.Entry<Integer,Integer> mapElement : string2Frequency.entrySet()) {
            int key = mapElement.getKey();
            if (mapElement.getValue() != string1Frequency.get(key)) {
                return false;
            }
        }
        return true;
    }

    public boolean basicCheck(String word1, String word2) {
        if (word1.length() != word2.length()) {
            return false;
        }

        HashSet<Character> word1Char = new HashSet<Character>();
        HashSet<Character> word2Char = new HashSet<Character>();

        for(int i=0; i<word1.length(); i++) {
            word1Char.add(word1.charAt(i));
        }

        for(int i=0; i<word2.length(); i++) {
            word2Char.add(word2.charAt(i));
        }

        if (word1Char.size() != word2Char.size()) {
            return false;
        }

        for(int i=0; i<26; i++) {
            if (word1Char.contains((char)(i+'a')) 
            != word2Char.contains((char)(i+'a'))) {
                return false;
            }
        }
        return true;
    }
}

Unpacking the Solution

  1. Basic Check:
    • The basicCheck method ensures a preliminary check for the lengths and character sets of the two words. If these criteria are not met, the words cannot be considered close.
  2. Frequency Distribution:
    • The frequency of characters in each word is computed and stored in string1Frequency and string2Frequency using HashMaps.
  3. Comparison of Frequencies:
    • The frequencies of each character in both words are compared. If any discrepancy is found, the words are deemed not close.

The intricacies of the Solution class illuminate the subtle art of determining string closeness. By scrutinizing both character sets and frequency distributions, the closeStrings method navigates the challenge with finesse. As you venture into the realm of string comparison, may this Java solution serve as a guide in unraveling the complexities and nuances that arise when assessing the closeness of two words.

Related Post