Groups of Strings Problem
Description
LeetCode Problem 2157.
You are given a 0-indexed array of strings words. Each string consists of lowercase English letters only. No letter occurs more than once in any string of words.
Two strings s1 and s2 are said to be connected if the set of letters of s2 can be obtained from the set of letters of s1 by any one of the following operations:
- Adding exactly one letter to the set of the letters of
s1. - Deleting exactly one letter from the set of the letters of
s1. - Replacing exactly one letter from the set of the letters of
s1with any letter, including itself.
The array words can be divided into one or more non-intersecting groups. A string belongs to a group if any one of the following is true:
- It is connected to at least one other string of the group.
- It is the only string present in the group.
Note that the strings in words should be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique.
Return an array ans of size 2 where:
ans[0]is the maximum number of groupswordscan be divided into, andans[1]is the size of the largest group.
Example 1:
Input: words = ["a","b","ab","cde"]
Output: [2,3]
Explanation:
- words[0] can be used to obtain words[1] (by replacing 'a' with 'b'), and words[2] (by adding 'b'). So words[0] is connected to words[1] and words[2].
- words[1] can be used to obtain words[0] (by replacing 'b' with 'a'), and words[2] (by adding 'a'). So words[1] is connected to words[0] and words[2].
- words[2] can be used to obtain words[0] (by deleting 'b'), and words[1] (by deleting 'a'). So words[2] is connected to words[0] and words[1].
- words[3] is not connected to any string in words.
Thus, words can be divided into 2 groups ["a","b","ab"] and ["cde"]. The size of the largest group is 3.
Example 2:
Input: words = ["a","ab","abc"]
Output: [1,3]
Explanation:
- words[0] is connected to words[1].
- words[1] is connected to words[0] and words[2].
- words[2] is connected to words[1].
Since all strings are connected to each other, they should be grouped together.
Thus, the size of the largest group is 3.
Constraints:
1 <= words.length <= 2 * 10^41 <= words[i].length <= 26words[i]consists of lowercase English letters only.- No letter occurs more than once in
words[i].
Solution
Approach: Bitmask + Union-Find
Each word is a set of distinct letters, so represent it as a 26-bit mask; words with the same mask are in the same group. Two masks are connected when one is obtained from the other by adding a letter, deleting a letter, or replacing a letter.
Use union-find over the distinct masks, with a size for each group. For every mask x, try every bit i: union x with x xor bit i (add or delete). If bit i is set in x, also try clearing it and setting each unset bit j (replace). Only masks that actually occur are merged. Track the number of groups (starting from the number of words, minus one for each repeated mask and for each successful union) and the largest group size.
Complexity: O(n * 26^2) time and O(n) extra space.
from collections import Counter
class Solution:
def groupStrings(self, words: List[str]) -> List[int]:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
def union(a, b):
nonlocal mx, n
if b not in p:
return
pa, pb = find(a), find(b)
if pa == pb:
return
p[pa] = pb
size[pb] += size[pa]
mx = max(mx, size[pb])
n -= 1
p = {}
size = Counter()
n = len(words)
mx = 0
for word in words:
x = 0
for c in word:
x |= 1 << (ord(c) - ord('a'))
p[x] = x
size[x] += 1
mx = max(mx, size[x])
if size[x] > 1:
n -= 1
for x in p.keys():
for i in range(26):
union(x, x ^ (1 << i))
if (x >> i) & 1:
for j in range(26):
if ((x >> j) & 1) == 0:
union(x, x ^ (1 << i) | (1 << j))
return [n, mx]