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:
Example 2: Input: words = ["aabb","ab","ba"] Output: 3 Explanation: There are 3 pairs that satisfy the conditions:
Example 3: Input: words = ["nba","cba","dba"] Output: 0 Explanation: Since there does not exist any pair that satisfies the conditions, we return 0.
def similar_pairs(words):
count = 0
for i in range(len(words)):
for j in range(i + 1, len(words)):
if set(words[i]) == set(words[j]):
count += 1
return count
# Example Usage
words1 = ["aba","aabb","abcd","bac","aabc"]
print(f"Number of similar pairs in {words1}: {similar_pairs(words1)}")
words2 = ["aabb","ab","ba"]
print(f"Number of similar pairs in {words2}: {similar_pairs(words2)}")
words3 = ["nba","cba","dba"]
print(f"Number of similar pairs in {words3}: {similar_pairs(words3)}")
The most straightforward approach is to iterate through all possible pairs of words and, for each pair, check if they are similar. This involves comparing the unique characters in each word. The naive solution directly implements this approach.
The provided Python code implements the optimal solution. It iterates through all possible pairs of words and checks if the sets of characters in each word are equal. Using sets for comparison is efficient because set comparison has an average time complexity of O(min(len(set1), len(set2))). This avoids nested loops for character-by-character comparison.
Outer Loop: The outer loop iterates n
times, where n
is the number of words in the input list.
Inner Loop: The inner loop iterates from i + 1
to n
, so it runs approximately n/2
times on average.
Set Conversion: Creating a set from a word of length m
takes O(m) time.
Set Comparison: Comparing two sets takes O(k) time on average, where k
is the number of unique characters in the smaller set.
Therefore, the overall time complexity is O(n^2 * m), where n
is the number of words and m
is the average length of the words. More precisely, each comparison is O(min(len(set(word1)), len(set(word2)))) which is upper bounded by O(m). The nested loops contribute O(n^2).
Sets: For each pair of words, we create two sets to store the unique characters. The space required for each set is O(m), where m
is the average length of the words.
Variables: We use a constant amount of extra space for variables like count
, i
, and j
.
Therefore, the overall space complexity is O(m), where m
is the average length of the words. This is because, in each iteration of the inner loop, we create two sets with a size proportional to the length of the input words.
words
is empty, the code should return 0 because there are no pairs to compare.set()
function automatically removes duplicates.