Count Similar String Groups
Updated
Problem statement
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[]) → intExamples
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 = 1The two strings differ in exactly two positions, so one swap makes them equal.
Constraints
- All strings in
strsare anagrams of each other. - All strings have the same length.
More Amazon Onsite Interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)