[LeetCode] Groups of Strings

2157. Groups of Strings

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 s1 with 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 groups words can be divided into, and
  • ans[1] is the size of the largest group.
  • AC on dfs solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
unordered_map<int, int> bits;
int dfs(int bi) {
auto it = bits.find(bi);
if(it == bits.end()) return 0;
int res = it->second;
bits.erase(it);
for(int i = 0; i < 26; i++) {
res += dfs(bi ^ (1<<i));
for(int j = i + 1; j < 26; j++) {
if(((bi >> i) & 1) != ((bi >> j) & 1))
res += dfs(bi ^ (1<<i) ^ (1<<j));
}
}
return res;
}
public:
vector<int> groupStrings(vector<string>& words) {
int sz = 0, ma = 0;
for(auto& str: words) {
int bi = 0;
for(auto& ch : str) bi |= 1<<(ch-'a');
bits[bi]++;
}
while(!bits.empty()) {
auto count = dfs(bits.begin()->first);
ma = max(ma, count);
sz += count > 0;
}

return {sz, ma};
}
};
  • TLE on bfs solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
vector<int> groupStrings(vector<string>& words) {
unordered_map<int, int> bits;
int sz = 0, ma = 0;
for(auto& str: words) {
int bi = 0;
for(auto& ch : str) bi |= 1<<(ch-'a');
bits[bi]++;
}
queue<int> qq;
while(!bits.empty()) {
auto it = bits.begin();
int bi = it->first, counts = it->second;
bits.erase(it);
qq.push(bi);
bits.erase(bi);
while(!qq.empty()) {
bi = qq.front(); qq.pop();
for(int i = 0; i < 26; i++) {
int similar = bi ^ (1<<i);
for(int j = i + 1; j < 26; j++) {
if(((bi >> i) & 1) != ((bi >> j) & 1)) {
int similar2 = similar ^ (1 << j);
if (bits[similar2]) {
counts += bits[similar2];
qq.push(similar2);
bits.erase(similar2);
}
}
}

if (bits[similar]) {
counts += bits[similar];
qq.push(similar);
bits.erase(similar);
}
}
}
sz++;
ma = max(ma, counts);
}
return {sz, ma};
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/10/PS/LeetCode/groups-of-strings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.