Count Pairs Of Similar Strings

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 <= words.length - 1 and the two strings words[i] and words[j] are similar.

Example 1
Inputwords = ["aba","aabb","abcd","bac","aabc"]
Output2
There are 2 valid pairs: indices 0 and 1 both use only 'a' and 'b', and indices 3 and 4 both use only 'a', 'b', and 'c'.
Example 2
Inputwords = ["aabb","ab","ba"]
Output3
All three pairs consist only of the characters 'a' and 'b', so every pair is similar.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] consist of only lowercase English letters.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate