All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2501_2600.s2506_count_pairs_of_similar_strings.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2501_2600.s2506_count_pairs_of_similar_strings;

// #Easy #Array #String #Hash_Table #2023_03_20_Time_6_ms_(85.15%)_Space_42.4_MB_(59.86%)

import java.util.HashMap;
import java.util.Map;

/**
 * 2506 - Count Pairs Of Similar Strings\.
 *
 * Easy
 *
 * You are given a **0-indexed** string array `words`.
 *
 * Two strings are **similar** if they consist of the same characters.
 *
 * *   For example, `"abca"` and `"cba"` are similar since both consist of characters `'a'`, `'b'`, and `'c'`.
 * *   However, `"abacba"` and `"bcfd"` are not similar since they do not consist of the same characters.
 *
 * Return _the number of pairs_ `(i, j)` _such that_ `0 <= i < j <= word.length - 1` _and the two strings_ `words[i]` _and_ `words[j]` _are similar_.
 *
 * **Example 1:**
 *
 * **Input:** words = ["aba","aabb","abcd","bac","aabc"]
 *
 * **Output:** 2
 *
 * **Explanation:** There are 2 pairs that satisfy the conditions: 
 *
 * - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'. 
 *
 * - i = 3 and j = 4 : both words[3] and words[4] only consist of characters 'a', 'b', and 'c'.
 *
 * **Example 2:**
 *
 * **Input:** words = ["aabb","ab","ba"]
 *
 * **Output:** 3
 *
 * **Explanation:** There are 3 pairs that satisfy the conditions: 
 *
 * - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'. 
 *
 * - i = 0 and j = 2 : both words[0] and words[2] only consist of characters 'a' and 'b'. 
 *
 * - i = 1 and j = 2 : both words[1] and words[2] only consist of characters 'a' and 'b'.
 *
 * **Example 3:**
 *
 * **Input:** words = ["nba","cba","dba"]
 *
 * **Output:** 0
 *
 * **Explanation:** Since there does not exist any pair that satisfies the conditions, we return 0.
 *
 * **Constraints:**
 *
 * *   `1 <= words.length <= 100`
 * *   `1 <= words[i].length <= 100`
 * *   `words[i]` consist of only lowercase English letters.
**/
public class Solution {
    public int similarPairs(String[] words) {
        int len = words.length;
        if (len == 1) {
            return 0;
        }
        byte[][] alPh = new byte[len][26];
        Map map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            String word = words[i];
            for (char c : word.toCharArray()) {
                int idx = c - 'a';
                if (alPh[i][idx] == 0) {
                    alPh[i][idx]++;
                }
            }
            String s = new String(alPh[i]);
            if (map.containsKey(s)) {
                map.put(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
            }
        }
        int pairs = 0;
        for (Map.Entry entry : map.entrySet()) {
            int freq = entry.getValue();
            if (freq > 1) {
                pairs += (freq * (freq - 1)) / 2;
            }
        }
        return pairs;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy