Show problems as
Classic problem #2506EasyArrayHash TableStringBit ManipulationCounting

Count Pairs Of Similar Strings

Real-world version: Pairs of Labels Using the Same Letters
Tracked in this browser only
Read Count Pairs Of Similar Strings on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A shelf holds the labels labels. Two labels are alike when each uses exactly the same set of letters as the other, however many times each letter turns up in either.

Return how many pairs of positions i < j hold labels that are alike.

Examples

Example 1

Input
labels = ["abc", "cba", "bca", "acb"]
Output
6

All four labels use exactly the letters a, b and c, so all six pairs are alike.

Example 2

Input
labels = ["a", "aa", "aaa"]
Output
3

Each label uses only the letter a, however many times, so all three pairs are alike.

Example 3

Input
labels = ["ab", "cd", "ef", "ab", "cd"]
Output
2

Only the two ab labels are alike, and the two cd labels.

Constraints

  • 1 <= labels.length <= 100
  • 1 <= labels[i].length <= 100
  • each label holds only lowercase English letters

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def similar_pairs(labels: list[str]) -> int:
Java
public int similarPairs(String[] labels)