Classic problem #2506EasyArrayHash TableStringBit ManipulationCounting
Count Pairs Of Similar Strings
Real-world version: Pairs of Labels Using the Same LettersTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Count the Number of Consistent StringsReal-world version: Labels the Press Can SetEasy
- Maximum Score Words Formed by LettersReal-world version: Best Tile Rack PlayHard
- Find Words That Can Be Formed by CharactersReal-world version: Signs Spellable From A Tile TrayEasy
- Most Common WordReal-world version: The Commonest Allowed WordEasy