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
String Signature Grouping ProcessStep 1: Create Signatures"abc"โ†’abc"bac"โ†’abc"def"โ†’defStep 2: Group by SignatureGroup "abc""abc", "bac"Count: 2Group "def""def"Count: 1Step 3: Calculate PairsGroup "abc": 2 stringsPairs = 2ร—1/2 = 1Group "def": 1 stringPairs = 0Step 4: Sum ResultsTotal Pairs1 + 0 = 1
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map storing signatures for up to n unique character sets

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค words.length โ‰ค 100
  • 1 โ‰ค words[i].length โ‰ค 100
  • words[i] consists of only lowercase English letters
Asked in
Amazon 15 Google 12 Meta 8 Microsoft 6
24.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen