Skip to content

Commit eb1ff32

Browse files
committed
feat: add solutions to lc problem: No.1239
No.1239.Maximum Length of a Concatenated String with Unique Characters
1 parent 6ffd3b2 commit eb1ff32

File tree

6 files changed

+211
-128
lines changed

6 files changed

+211
-128
lines changed

solution/1200-1299/1239.Maximum Length of a Concatenated String with Unique Characters/README.md

+74-42
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:位运算 + 状态压缩**
62+
6163
状态压缩,用一个 32 位数记录字母的出现情况,`masks` 存储之前枚举的字符串。
6264

65+
时间复杂度 $O(2^n + L)$,空间复杂度 $O(2^n)$。其中 $n$ 和 $L$ 分别是字符串数组的长度和字符串数组中字符串的长度之和。
66+
6367
<!-- tabs:start -->
6468

6569
### **Python3**
@@ -69,30 +73,22 @@
6973
```python
7074
class Solution:
7175
def maxLength(self, arr: List[str]) -> int:
72-
def ones_count(x):
73-
c = 0
74-
while x:
75-
x &= x - 1
76-
c += 1
77-
return c
78-
7976
ans = 0
8077
masks = [0]
8178
for s in arr:
8279
mask = 0
83-
for ch in s:
84-
ch = ord(ch) - ord('a')
85-
if (mask >> ch) & 1 == 1:
80+
for c in s:
81+
i = ord(c) - ord('a')
82+
if mask >> i & 1:
8683
mask = 0
8784
break
88-
mask |= 1 << ch
85+
mask |= 1 << i
8986
if mask == 0:
9087
continue
9188
for m in masks:
9289
if m & mask == 0:
9390
masks.append(m | mask)
94-
ans = max(ans, ones_count(m | mask))
95-
91+
ans = max(ans, (m | mask).bit_count())
9692
return ans
9793
```
9894

@@ -106,66 +102,102 @@ class Solution {
106102
int ans = 0;
107103
List<Integer> masks = new ArrayList<>();
108104
masks.add(0);
109-
110-
loop:
111-
for (String s : arr) {
105+
for (var s : arr) {
112106
int mask = 0;
113-
for (char ch : s.toCharArray()) {
114-
ch -= 'a';
115-
if (((mask >> ch) & 1) == 1) {
116-
continue loop;
107+
for (int i = 0; i < s.length(); ++i) {
108+
int j = s.charAt(i) - 'a';
109+
if (((mask >> j) & 1) == 1) {
110+
mask = 0;
111+
break;
117112
}
118-
mask |= 1 << ch;
113+
mask |= 1 << j;
114+
}
115+
if (mask == 0) {
116+
continue;
119117
}
120118
int n = masks.size();
121-
for (int i = 0; i < n; i++) {
119+
for (int i = 0; i < n; ++i) {
122120
int m = masks.get(i);
123121
if ((m & mask) == 0) {
124122
masks.add(m | mask);
125123
ans = Math.max(ans, Integer.bitCount(m | mask));
126124
}
127125
}
128126
}
129-
130127
return ans;
131128
}
132129
}
133130
```
134131

135-
### **Go**
132+
### **C++**
136133

137-
```go
138-
func maxLength(arr []string) int {
134+
```cpp
135+
class Solution {
136+
public:
137+
int maxLength(vector<string>& arr) {
138+
int ans = 0;
139+
vector<int> masks = {0};
140+
for (auto& s : arr) {
141+
int mask = 0;
142+
for (auto& c : s) {
143+
int i = c - 'a';
144+
if (mask >> i & 1) {
145+
mask = 0;
146+
break;
147+
}
148+
mask |= 1 << i;
149+
}
150+
if (mask == 0) {
151+
continue;
152+
}
153+
int n = masks.size();
154+
for (int i = 0; i < n; ++i) {
155+
int m = masks[i];
156+
if ((m & mask) == 0) {
157+
masks.push_back(m | mask);
158+
ans = max(ans, __builtin_popcount(m | mask));
159+
}
160+
}
161+
}
162+
return ans;
163+
}
164+
};
165+
```
139166
140-
max := func(x, y int) int {
141-
if x > y {
142-
return x
143-
}
144-
return y
145-
}
167+
### **Go**
146168
147-
ans := 0
169+
```go
170+
func maxLength(arr []string) (ans int) {
148171
masks := []int{0}
149-
150-
loop:
151172
for _, s := range arr {
152173
mask := 0
153-
for _, ch := range s {
154-
ch -= 'a'
155-
if (mask>>ch)&1 == 1 {
156-
continue loop
174+
for _, c := range s {
175+
i := int(c - 'a')
176+
if mask>>i&1 == 1 {
177+
mask = 0
178+
break
157179
}
158-
mask |= 1 << ch
180+
mask |= 1 << i
159181
}
160-
for _, m := range masks {
182+
if mask == 0 {
183+
continue
184+
}
185+
n := len(masks)
186+
for _, m := range masks[:n] {
161187
if m&mask == 0 {
162188
masks = append(masks, m|mask)
163189
ans = max(ans, bits.OnesCount(uint(m|mask)))
164190
}
165191
}
166192
}
193+
return
194+
}
167195
168-
return ans
196+
func max(a, b int) int {
197+
if a > b {
198+
return a
199+
}
200+
return b
169201
}
170202
```
171203

solution/1200-1299/1239.Maximum Length of a Concatenated String with Unique Characters/README_EN.md

+70-42
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,22 @@ State compression uses a 32-bit number to record the appearance of letters, and
6262
```python
6363
class Solution:
6464
def maxLength(self, arr: List[str]) -> int:
65-
def ones_count(x):
66-
c = 0
67-
while x:
68-
x &= x - 1
69-
c += 1
70-
return c
71-
7265
ans = 0
7366
masks = [0]
7467
for s in arr:
7568
mask = 0
76-
for ch in s:
77-
ch = ord(ch) - ord('a')
78-
if (mask >> ch) & 1 == 1:
69+
for c in s:
70+
i = ord(c) - ord('a')
71+
if mask >> i & 1:
7972
mask = 0
8073
break
81-
mask |= 1 << ch
74+
mask |= 1 << i
8275
if mask == 0:
8376
continue
8477
for m in masks:
8578
if m & mask == 0:
8679
masks.append(m | mask)
87-
ans = max(ans, ones_count(m | mask))
88-
80+
ans = max(ans, (m | mask).bit_count())
8981
return ans
9082
```
9183

@@ -97,66 +89,102 @@ class Solution {
9789
int ans = 0;
9890
List<Integer> masks = new ArrayList<>();
9991
masks.add(0);
100-
101-
loop:
102-
for (String s : arr) {
92+
for (var s : arr) {
10393
int mask = 0;
104-
for (char ch : s.toCharArray()) {
105-
ch -= 'a';
106-
if (((mask >> ch) & 1) == 1) {
107-
continue loop;
94+
for (int i = 0; i < s.length(); ++i) {
95+
int j = s.charAt(i) - 'a';
96+
if (((mask >> j) & 1) == 1) {
97+
mask = 0;
98+
break;
10899
}
109-
mask |= 1 << ch;
100+
mask |= 1 << j;
101+
}
102+
if (mask == 0) {
103+
continue;
110104
}
111105
int n = masks.size();
112-
for (int i = 0; i < n; i++) {
106+
for (int i = 0; i < n; ++i) {
113107
int m = masks.get(i);
114108
if ((m & mask) == 0) {
115109
masks.add(m | mask);
116110
ans = Math.max(ans, Integer.bitCount(m | mask));
117111
}
118112
}
119113
}
120-
121114
return ans;
122115
}
123116
}
124117
```
125118

126-
### **Go**
119+
### **C++**
127120

128-
```go
129-
func maxLength(arr []string) int {
121+
```cpp
122+
class Solution {
123+
public:
124+
int maxLength(vector<string>& arr) {
125+
int ans = 0;
126+
vector<int> masks = {0};
127+
for (auto& s : arr) {
128+
int mask = 0;
129+
for (auto& c : s) {
130+
int i = c - 'a';
131+
if (mask >> i & 1) {
132+
mask = 0;
133+
break;
134+
}
135+
mask |= 1 << i;
136+
}
137+
if (mask == 0) {
138+
continue;
139+
}
140+
int n = masks.size();
141+
for (int i = 0; i < n; ++i) {
142+
int m = masks[i];
143+
if ((m & mask) == 0) {
144+
masks.push_back(m | mask);
145+
ans = max(ans, __builtin_popcount(m | mask));
146+
}
147+
}
148+
}
149+
return ans;
150+
}
151+
};
152+
```
130153
131-
max := func(x, y int) int {
132-
if x > y {
133-
return x
134-
}
135-
return y
136-
}
154+
### **Go**
137155
138-
ans := 0
156+
```go
157+
func maxLength(arr []string) (ans int) {
139158
masks := []int{0}
140-
141-
loop:
142159
for _, s := range arr {
143160
mask := 0
144-
for _, ch := range s {
145-
ch -= 'a'
146-
if (mask>>ch)&1 == 1 {
147-
continue loop
161+
for _, c := range s {
162+
i := int(c - 'a')
163+
if mask>>i&1 == 1 {
164+
mask = 0
165+
break
148166
}
149-
mask |= 1 << ch
167+
mask |= 1 << i
150168
}
151-
for _, m := range masks {
169+
if mask == 0 {
170+
continue
171+
}
172+
n := len(masks)
173+
for _, m := range masks[:n] {
152174
if m&mask == 0 {
153175
masks = append(masks, m|mask)
154176
ans = max(ans, bits.OnesCount(uint(m|mask)))
155177
}
156178
}
157179
}
180+
return
181+
}
158182
159-
return ans
183+
func max(a, b int) int {
184+
if a > b {
185+
return a
186+
}
187+
return b
160188
}
161189
```
162190

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int maxLength(vector<string>& arr) {
4+
int ans = 0;
5+
vector<int> masks = {0};
6+
for (auto& s : arr) {
7+
int mask = 0;
8+
for (auto& c : s) {
9+
int i = c - 'a';
10+
if (mask >> i & 1) {
11+
mask = 0;
12+
break;
13+
}
14+
mask |= 1 << i;
15+
}
16+
if (mask == 0) {
17+
continue;
18+
}
19+
int n = masks.size();
20+
for (int i = 0; i < n; ++i) {
21+
int m = masks[i];
22+
if ((m & mask) == 0) {
23+
masks.push_back(m | mask);
24+
ans = max(ans, __builtin_popcount(m | mask));
25+
}
26+
}
27+
}
28+
return ans;
29+
}
30+
};

0 commit comments

Comments
 (0)