Reported April 2026
Amazonunion find

Count Similar String Groups

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reportedly served this one in April 2026, and the whole solution hinges on union-find. Count Similar String Groups looks like a string problem, but it's really a connected components problem in disguise. Each string is a node. An edge exists when two strings differ in zero or exactly two positions. Count the components and you're done. If you've got an OA coming up, this is a pattern worth recognizing on sight. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you the structure in real time.

The problem

You are given an array of strings strs. All strings are anagrams of each other.
Two strings are considered similar if they are identical, or if you can make them equal by swapping exactly two characters in one of the strings.
Similarity is transitive: if string a is similar to b, and b is similar to c, then all three belong to the same group.
Return the number of groups of similar strings.

Function
countSimilarStringGroups(strs: String[]) → int

Examples
Example 1
strs = ["tars", "rats", "arts", "star"]
return = 2
"tars" is similar to "rats", and "rats" is similar to "arts", so they form one group. "star" forms another group.
Example 2
strs = ["omv", "ovm"]
return = 1
The two strings differ in exactly two positions, so one swap makes them equal.

Constraints
All strings in strs are anagrams of each other.
All strings have the same length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to stop thinking about swaps and start thinking about graph connectivity. Write a helper that compares two strings and counts mismatched positions. If the count is 0 or 2, they're similar. Since all strings are anagrams, two mismatches always means one swap fixes it. Then run union-find, or DFS, over every pair and count the distinct roots. Complexity is O(n^2 * L) for n strings of length L. The common pitfall is checking only adjacent strings in the input order, which misses transitive links. Another is treating 1 mismatch as valid. It can't happen with anagrams, but a sloppy check like diff <= 2 still passes. Be exact with 0 or 2. Also don't try to generate all swaps per string unless L is tiny. If the live OA has you freezing on the union-find boilerplate, StealthCoder is the hedge that gives you a clean template to adapt.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Count Similar String Groups cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as similar string groups. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Similar String Groups FAQ

What's the trick to Count Similar String Groups?+

Model it as a graph. Each string is a node, and two strings connect if they differ in 0 or 2 positions. The answer is the number of connected components. Union-find or DFS both work. The swap logic is just the edge test.

How hard is this Amazon OA question really?+

Medium to hard on paper, but it's mostly a standard union-find problem with a small string comparison helper. If you've written union-find with path compression before, you can finish it quickly. The difficulty is spotting the graph framing.

Should I use union-find or DFS?+

Either passes. Union-find is shorter to reason about when you're counting components from pairwise checks. DFS needs an adjacency scan per node, which is the same O(n^2 * L) work. Pick the one you can write without bugs under pressure.

What's the time complexity I should expect?+

O(n^2 * L) for comparing every pair of n strings with length L, plus near-constant union-find operations. Alternatively, if n is huge and L is tiny, generating swaps per string can be faster. Check the constraints first and choose accordingly.

How do I prepare for this in 48 hours?+

Write union-find from memory twice, with path compression and a component counter. Then solve this one by hand with the mismatch helper. Also review the number of islands style connected components problems. That covers the pattern without cramming unrelated topics.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it