Count Similar Pairs
Learn this problemProblem statement
Two strings are said to be similar if they are composed of the same characters. For example "abaca" and "cba" are similar since both of them are composed of characters 'a', 'b' and 'c'. However "abaca" and "bcd" are not similar since they do not share all of the same letters.
Given an array of strings words of length n, find the number of pairs of strings that are similar.
Note:
- Each string is composed of lowercase English characters only.
- Pairs are considered index-wise, i.e., two equal strings at different indices are counted as separated pairs.
- A pair at indices
(i, j)is the same as the pair at(j, i).
Function
countSimilarPairs(words: String[]) → long
Complete the function countSimilarPairs in the editor below.
countSimilarPairs has the following parameter:
string words[n]: an array ofnstrings
Returns
long integer: the number of similar pairs
Examples
Example 1
words = ["xyz", "foo", "of"]return = 1
Here, the strings "foo" and "of" are similar because they are composed of the same characters ['o', 'f']. There are no other similar pairs so the answer is 1.
Constraints
- 1 ≤
n≤ 10^5 - The Sum of the lengths of all strings does not exceed 10^6.
- All strings consist of lowercase English characters only.
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026