Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed string array words.
Two strings are similar if they consist of the same characters.
"abca" and "cba" are similar since both consist of characters 'a', 'b', and 'c'."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: - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'. - i = 3 and j = 4 : both words[3] and words[4] only consist of characters 'a', 'b', and 'c'.
Example 2:
Input: words = ["aabb","ab","ba"] Output: 3 Explanation: There are 3 pairs that satisfy the conditions: - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'. - i = 0 and j = 2 : both words[0] and words[2] only consist of characters 'a' and 'b'. - i = 1 and j = 2 : both words[1] and words[2] only consist of characters 'a' and 'b'.
Example 3:
Input: words = ["nba","cba","dba"] Output: 0 Explanation: Since there does not exist any pair that satisfies the conditions, we return 0.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 100words[i] consist of only lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
["aba","aabb","abcd","bac","aabc"]
["aabb","ab","ba"]
["nba","cba","dba"]
sort-characters-by-frequency)count-the-number-of-consistent-strings)number-of-good-paths)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2506: Count Pairs Of Similar Strings
class Solution {
public int similarPairs(String[] words) {
int ans = 0;
Map<Integer, Integer> cnt = new HashMap<>();
for (var s : words) {
int x = 0;
for (char c : s.toCharArray()) {
x |= 1 << (c - 'a');
}
ans += cnt.merge(x, 1, Integer::sum) - 1;
}
return ans;
}
}
// Accepted solution for LeetCode #2506: Count Pairs Of Similar Strings
func similarPairs(words []string) (ans int) {
cnt := map[int]int{}
for _, s := range words {
x := 0
for _, c := range s {
x |= 1 << (c - 'a')
}
ans += cnt[x]
cnt[x]++
}
return
}
# Accepted solution for LeetCode #2506: Count Pairs Of Similar Strings
class Solution:
def similarPairs(self, words: List[str]) -> int:
ans = 0
cnt = Counter()
for s in words:
x = 0
for c in map(ord, s):
x |= 1 << (c - ord("a"))
ans += cnt[x]
cnt[x] += 1
return ans
// Accepted solution for LeetCode #2506: Count Pairs Of Similar Strings
use std::collections::HashMap;
impl Solution {
pub fn similar_pairs(words: Vec<String>) -> i32 {
let mut ans = 0;
let mut cnt: HashMap<i32, i32> = HashMap::new();
for s in words {
let mut x = 0;
for c in s.chars() {
x |= 1 << ((c as u8) - b'a');
}
ans += cnt.get(&x).unwrap_or(&0);
*cnt.entry(x).or_insert(0) += 1;
}
ans
}
}
// Accepted solution for LeetCode #2506: Count Pairs Of Similar Strings
function similarPairs(words: string[]): number {
let ans = 0;
const cnt = new Map<number, number>();
for (const s of words) {
let x = 0;
for (const c of s) {
x |= 1 << (c.charCodeAt(0) - 97);
}
ans += cnt.get(x) || 0;
cnt.set(x, (cnt.get(x) || 0) + 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.