Count Pairs Of Similar Strings - Problem
You are given a 0-indexed string array words.
Two strings are similar if they consist of the same characters (regardless of frequency or order).
- 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.
Goal: Return the number of pairs (i, j) such that 0 <= i < j < words.length and the two strings words[i] and words[j] are similar.
Think of this as finding strings that have the same "character signature" - the unique set of characters they contain!
Input & Output
example_1.py โ Basic Case
$
Input:
["aba", "aabb", "abcd", "bac", "aabc"]
โบ
Output:
2
๐ก Note:
The pairs of similar strings are: ("aba", "aabb") - both have characters {a,b}, and ("abcd", "aabc") - both have characters {a,b,c,d}. Note that "aba" and "bac" are also similar (both have {a,b,c}), wait let me recalculate... Actually: "aba"โ{a,b}, "aabb"โ{a,b}, "abcd"โ{a,b,c,d}, "bac"โ{a,b,c}, "aabc"โ{a,b,c}. So pairs are: (0,1) both {a,b}, and (3,4) both {a,b,c}.
example_2.py โ All Different
$
Input:
["aaa", "bb", "ccc"]
โบ
Output:
0
๐ก Note:
No two strings are similar. "aaa" has {a}, "bb" has {b}, and "ccc" has {c}. All have different character sets, so no pairs are formed.
example_3.py โ All Same Signature
$
Input:
["abc", "bac", "cab", "acb"]
โบ
Output:
6
๐ก Note:
All four strings have the same character set {a,b,c}, so they're all similar to each other. With 4 strings, we get C(4,2) = 4ร3/2 = 6 pairs: (0,1), (0,2), (0,3), (1,2), (1,3), (2,3).
Visualization
Tap to expand
Understanding the Visualization
1
Extract Signatures
Convert each string to a unique signature representing its character set
2
Group by Signature
Collect all strings that have the same signature
3
Count Pairs per Group
For each group with n strings, calculate nร(n-1)/2 pairs
4
Sum All Groups
Add up pairs from all signature groups to get final answer
Key Takeaway
๐ฏ Key Insight: Convert the comparison problem into a counting problem by grouping strings with identical character signatures, then use the combination formula to count pairs efficiently.
Time & Space Complexity
Time Complexity
O(n ร m)
n strings, each taking O(m) time to create signature where m is string length
โ Linear Growth
Space Complexity
O(n)
Hash map storing signatures for up to n unique character sets
โก Linearithmic Space
Constraints
- 1 โค words.length โค 100
- 1 โค words[i].length โค 100
- words[i] consists of only lowercase English letters
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code