|
70 | 70 |
|
71 | 71 | <!-- 这里可写通用的实现逻辑 -->
|
72 | 72 |
|
| 73 | +**方法一:状态压缩(位运算) + 并查集** |
| 74 | + |
73 | 75 | <!-- tabs:start -->
|
74 | 76 |
|
75 | 77 | ### **Python3**
|
76 | 78 |
|
77 | 79 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
78 | 80 |
|
79 | 81 | ```python
|
80 |
| - |
| 82 | +class Solution: |
| 83 | + def groupStrings(self, words: List[str]) -> List[int]: |
| 84 | + def find(x): |
| 85 | + if p[x] != x: |
| 86 | + p[x] = find(p[x]) |
| 87 | + return p[x] |
| 88 | + |
| 89 | + def union(a, b): |
| 90 | + nonlocal mx, n |
| 91 | + if b not in p: |
| 92 | + return |
| 93 | + pa, pb = find(a), find(b) |
| 94 | + if pa == pb: |
| 95 | + return |
| 96 | + p[pa] = pb |
| 97 | + size[pb] += size[pa] |
| 98 | + mx = max(mx, size[pb]) |
| 99 | + n -= 1 |
| 100 | + |
| 101 | + p = {} |
| 102 | + size = Counter() |
| 103 | + n = len(words) |
| 104 | + mx = 0 |
| 105 | + for word in words: |
| 106 | + x = 0 |
| 107 | + for c in word: |
| 108 | + x |= 1 << (ord(c) - ord('a')) |
| 109 | + p[x] = x |
| 110 | + size[x] += 1 |
| 111 | + mx = max(mx, size[x]) |
| 112 | + if size[x] > 1: |
| 113 | + n -= 1 |
| 114 | + for x in p.keys(): |
| 115 | + for i in range(26): |
| 116 | + union(x, x ^ (1 << i)) |
| 117 | + if (x >> i) & 1: |
| 118 | + for j in range(26): |
| 119 | + if ((x >> j) & 1) == 0: |
| 120 | + union(x, x ^ (1 << i) | (1 << j)) |
| 121 | + return [n, mx] |
81 | 122 | ```
|
82 | 123 |
|
83 | 124 | ### **Java**
|
84 | 125 |
|
85 | 126 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
86 | 127 |
|
87 | 128 | ```java
|
| 129 | +class Solution { |
| 130 | + private Map<Integer, Integer> p; |
| 131 | + private Map<Integer, Integer> size; |
| 132 | + private int mx; |
| 133 | + private int n; |
| 134 | + |
| 135 | + public int[] groupStrings(String[] words) { |
| 136 | + p = new HashMap<>(); |
| 137 | + size = new HashMap<>(); |
| 138 | + n = words.length; |
| 139 | + mx = 0; |
| 140 | + for (String word : words) { |
| 141 | + int x = 0; |
| 142 | + for (char c : word.toCharArray()) { |
| 143 | + x |= 1 << (c - 'a'); |
| 144 | + } |
| 145 | + p.put(x, x); |
| 146 | + size.put(x, size.getOrDefault(x, 0) + 1); |
| 147 | + mx = Math.max(mx, size.get(x)); |
| 148 | + if (size.get(x) > 1) { |
| 149 | + --n; |
| 150 | + } |
| 151 | + } |
| 152 | + for (int x : p.keySet()) { |
| 153 | + for (int i = 0; i < 26; ++i) { |
| 154 | + union(x, x ^ (1 << i)); |
| 155 | + if (((x >> i) & 1) != 0) { |
| 156 | + for (int j = 0; j < 26; ++j) { |
| 157 | + if (((x >> j) & 1) == 0) { |
| 158 | + union(x, x ^ (1 << i) | (1 << j)); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + return new int[]{n, mx}; |
| 165 | + } |
| 166 | + |
| 167 | + private int find(int x) { |
| 168 | + if (p.get(x) != x) { |
| 169 | + p.put(x, find(p.get(x))); |
| 170 | + } |
| 171 | + return p.get(x); |
| 172 | + } |
| 173 | + |
| 174 | + private void union(int a, int b) { |
| 175 | + if (!p.containsKey(b)) { |
| 176 | + return; |
| 177 | + } |
| 178 | + int pa = find(a), pb = find(b); |
| 179 | + if (pa == pb) { |
| 180 | + return; |
| 181 | + } |
| 182 | + p.put(pa, pb); |
| 183 | + size.put(pb, size.get(pb) + size.get(pa)); |
| 184 | + mx = Math.max(mx, size.get(pb)); |
| 185 | + --n; |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +### **C++** |
| 191 | + |
| 192 | +```cpp |
| 193 | +class Solution { |
| 194 | +public: |
| 195 | + int mx, n; |
| 196 | + |
| 197 | + vector<int> groupStrings(vector<string>& words) { |
| 198 | + unordered_map<int, int> p; |
| 199 | + unordered_map<int, int> size; |
| 200 | + mx = 0; |
| 201 | + n = words.size(); |
| 202 | + for (auto& word : words) |
| 203 | + { |
| 204 | + int x = 0; |
| 205 | + for (auto& c : word) x |= 1 << (c - 'a'); |
| 206 | + p[x] = x; |
| 207 | + ++size[x]; |
| 208 | + mx = max(mx, size[x]); |
| 209 | + if (size[x] > 1) --n; |
| 210 | + } |
| 211 | + for (auto& [x, _] : p) |
| 212 | + { |
| 213 | + for (int i = 0; i < 26; ++i) |
| 214 | + { |
| 215 | + unite(x, x ^ (1 << i), p, size); |
| 216 | + if ((x >> i) & 1) |
| 217 | + { |
| 218 | + for (int j = 0; j < 26; ++j) |
| 219 | + { |
| 220 | + if (((x >> j) & 1) == 0) unite(x, x ^ (1 << i) | (1 << j), p, size); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + return {n, mx}; |
| 226 | + } |
| 227 | + |
| 228 | + int find(int x, unordered_map<int, int>& p) { |
| 229 | + if (p[x] != x) p[x] = find(p[x], p); |
| 230 | + return p[x]; |
| 231 | + } |
| 232 | + |
| 233 | + void unite(int a, int b, unordered_map<int, int>& p, unordered_map<int, int>& size) { |
| 234 | + if (!p.count(b)) return; |
| 235 | + int pa = find(a, p), pb = find(b, p); |
| 236 | + if (pa == pb) return; |
| 237 | + p[pa] = pb; |
| 238 | + size[pb] += size[pa]; |
| 239 | + mx = max(mx, size[pb]); |
| 240 | + --n; |
| 241 | + } |
| 242 | +}; |
| 243 | +``` |
88 | 244 |
|
| 245 | +### **Go** |
| 246 | + |
| 247 | +```go |
| 248 | +func groupStrings(words []string) []int { |
| 249 | + p := map[int]int{} |
| 250 | + size := map[int]int{} |
| 251 | + mx, n := 0, len(words) |
| 252 | + var find func(int) int |
| 253 | + find = func(x int) int { |
| 254 | + if p[x] != x { |
| 255 | + p[x] = find(p[x]) |
| 256 | + } |
| 257 | + return p[x] |
| 258 | + } |
| 259 | + union := func(a, b int) { |
| 260 | + if _, ok := p[b]; !ok { |
| 261 | + return |
| 262 | + } |
| 263 | + pa, pb := find(a), find(b) |
| 264 | + if pa == pb { |
| 265 | + return |
| 266 | + } |
| 267 | + p[pa] = pb |
| 268 | + size[pb] += size[pa] |
| 269 | + mx = max(mx, size[pb]) |
| 270 | + n-- |
| 271 | + } |
| 272 | + |
| 273 | + for _, word := range words { |
| 274 | + x := 0 |
| 275 | + for _, c := range word { |
| 276 | + x |= 1 << (c - 'a') |
| 277 | + } |
| 278 | + p[x] = x |
| 279 | + size[x]++ |
| 280 | + mx = max(mx, size[x]) |
| 281 | + if size[x] > 1 { |
| 282 | + n-- |
| 283 | + } |
| 284 | + } |
| 285 | + for x := range p { |
| 286 | + for i := 0; i < 26; i++ { |
| 287 | + union(x, x^(1<<i)) |
| 288 | + if ((x >> i) & 1) != 0 { |
| 289 | + for j := 0; j < 26; j++ { |
| 290 | + if ((x >> j) & 1) == 0 { |
| 291 | + union(x, x^(1<<i)|(1<<j)) |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + return []int{n, mx} |
| 298 | +} |
| 299 | + |
| 300 | +func max(a, b int) int { |
| 301 | + if a > b { |
| 302 | + return a |
| 303 | + } |
| 304 | + return b |
| 305 | +} |
89 | 306 | ```
|
90 | 307 |
|
91 | 308 | ### **TypeScript**
|
|
0 commit comments