Similar String Groups

Given a list of strings, return the number of groups where two strings belong to the same group if they are identical or can become identical after swapping exactly two characters. DSU path compression keeps group merging efficient.

Input Format

strs = array of equal-length strings

Output Format

number of similar string groups

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

strs = ["tars","rats","arts","star"]

Output:

2

Explanation:

The strings split into two similarity groups.

Example 2:

Input:

strs = ["omv","ovm"]

Output:

1

Explanation:

The two strings differ by one swap.

Example 3:

Input:

strs = ["abc","abc","abc"]

Output:

1

Explanation:

Identical strings are all in the same group.

Loading...
Similar String Groups - Union Find DSA Problem