Skip to content

Commit e7092ae

Browse files
committed
feat: add solutions to lc problem: No.1255
No.1255.Maximum Score Words Formed by Letters
1 parent 1e0bde9 commit e7092ae

File tree

7 files changed

+356
-5
lines changed

7 files changed

+356
-5
lines changed

solution/1200-1299/1250.Check If It Is a Good Array/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@
4949

5050
**方法一:数学(裴蜀定理)**
5151

52-
我们可以考虑选取两个数的情况,若选取的数是 $a$ 和 $b$,那么根据题目的要求,我们需要满足 $a \times x + b \times y = 1$,其中 $x$ 和 $y$ 是任意整数。
52+
我们先可以考虑选取两个数的情况,若选取的数是 $a$ 和 $b$,那么根据题目的要求,我们需要满足 $a \times x + b \times y = 1$,其中 $x$ 和 $y$ 是任意整数。
5353

54-
而根据裴蜀定理,可以得知,如果 $a$ 和 $b$ 互质,那么上述等式一定有解。
54+
根据裴蜀定理,可以得知,如果 $a$ 和 $b$ 互质,那么上述等式一定有解。实际上,裴蜀定理也可以推广到多个数的情况,即如果 $a_1, a_2, \cdots, a_i$ 互质,那么 $a_1 \times x_1 + a_2 \times x_2 + \cdots + a_i \times x_i = 1$ 一定有解,其中 $x_1, x_2, \cdots, x_i$ 是任意整数。
55+
56+
因此,我们只需要判断在数组 `nums` 中是否存在 $i$ 个互质的数即可。两个数互质的充要条件是它们的最大公约数为 $1$。如果数组 `nums` 存在 $i$ 个互质的数,那么数组 `nums` 中的所有数的最大公约数也为 $1$。
57+
58+
所以我们将题目转化为:判断数组 `nums` 中的所有数的最大公约数是否为 $1$。遍历数组 `nums`,求出数组 `nums` 中的所有数的最大公约数即可。
5559

56-
因此,我们只需要判断在数组 `nums` 中是否存在两个互质的数即可。两个数互质的充要条件是它们的最大公约数为 $1$。如果数组 `nums` 存在 $a$ 和 $b$ 的最大公约数为 $1$,那么数组 `nums` 中的所有数的最大公约数也为 $1$。所以我们将题目转化为:判断数组 `nums` 中的所有数的最大公约数是否为 $1$ 即可。
5760

5861
时间复杂度 $O(n + log m)$,空间复杂度 $O(1)$,其中 $n$ 是数组 `nums` 的长度,而 $m$ 是数组 `nums` 中的最大值。
5962

solution/1200-1299/1255.Maximum Score Words Formed by Letters/README.md

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,146 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68+
**方法一:二进制枚举**
69+
70+
对于给定的单词集合,我们可以使用二进制枚举的方法,枚举出所有的单词组合,然后判断每个单词组合是否满足题目要求,如果满足则计算其得分,最后取得分最大的单词组合。
71+
72+
具体地,我们可以使用二进制的每一位来表示单词集合中的每一个单词是否被选中,如果第 $i$ 位为 $1$,则表示第 $i$ 个单词被选中,否则表示第 $i$ 个单词没有被选中。对于每一个二进制数,我们可以使用位运算来判断其对应的单词是否被选中。
73+
74+
时间复杂度 $(2^n \times n \times M)$,空间复杂度 $O(C)$。其中 $n$ 和 $M$ 分别为单词集合中单词的个数和单词的最大长度;而 $C$ 为字母表中字母的个数,本题中 $C=26$。
75+
6876
<!-- tabs:start -->
6977

7078
### **Python3**
7179

7280
<!-- 这里可写当前语言的特殊实现逻辑 -->
7381

7482
```python
75-
83+
class Solution:
84+
def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int:
85+
cnt = Counter(letters)
86+
n = len(words)
87+
ans = 0
88+
for i in range(1 << n):
89+
cur = Counter(''.join([words[j] for j in range(n) if i >> j & 1]))
90+
if all(v <= cnt[c] for c, v in cur.items()):
91+
t = sum(v * score[ord(c) - ord('a')] for c, v in cur.items())
92+
ans = max(ans, t)
93+
return ans
7694
```
7795

7896
### **Java**
7997

8098
<!-- 这里可写当前语言的特殊实现逻辑 -->
8199

82100
```java
101+
class Solution {
102+
public int maxScoreWords(String[] words, char[] letters, int[] score) {
103+
int[] cnt = new int[26];
104+
for (int i = 0; i < letters.length; ++i) {
105+
cnt[letters[i] - 'a']++;
106+
}
107+
int n = words.length;
108+
int ans = 0;
109+
for (int i = 0; i < 1 << n; ++i) {
110+
int[] cur = new int[26];
111+
for (int j = 0; j < n; ++j) {
112+
if (((i >> j) & 1) == 1) {
113+
for (int k = 0; k < words[j].length(); ++k) {
114+
cur[words[j].charAt(k) - 'a']++;
115+
}
116+
}
117+
}
118+
boolean ok = true;
119+
int t = 0;
120+
for (int j = 0; j < 26; ++j) {
121+
if (cur[j] > cnt[j]) {
122+
ok = false;
123+
break;
124+
}
125+
t += cur[j] * score[j];
126+
}
127+
if (ok && ans < t) {
128+
ans = t;
129+
}
130+
}
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
142+
int cnt[26]{};
143+
for (char& c : letters) {
144+
cnt[c - 'a']++;
145+
}
146+
int n = words.size();
147+
int ans = 0;
148+
for (int i = 0; i < 1 << n; ++i) {
149+
int cur[26]{};
150+
for (int j = 0; j < n; ++j) {
151+
if (i >> j & 1) {
152+
for (char& c : words[j]) {
153+
cur[c - 'a']++;
154+
}
155+
}
156+
}
157+
bool ok = true;
158+
int t = 0;
159+
for (int j = 0; j < 26; ++j) {
160+
if (cur[j] > cnt[j]) {
161+
ok = false;
162+
break;
163+
}
164+
t += cur[j] * score[j];
165+
}
166+
if (ok && ans < t) {
167+
ans = t;
168+
}
169+
}
170+
return ans;
171+
}
172+
};
173+
```
83174
175+
### **Go**
176+
177+
```go
178+
func maxScoreWords(words []string, letters []byte, score []int) (ans int) {
179+
cnt := [26]int{}
180+
for _, c := range letters {
181+
cnt[c-'a']++
182+
}
183+
n := len(words)
184+
for i := 0; i < 1<<n; i++ {
185+
cur := [26]int{}
186+
for j := 0; j < n; j++ {
187+
if i>>j&1 == 1 {
188+
for _, c := range words[j] {
189+
cur[c-'a']++
190+
}
191+
}
192+
}
193+
ok := true
194+
t := 0
195+
for i, v := range cur {
196+
if v > cnt[i] {
197+
ok = false
198+
break
199+
}
200+
t += v * score[i]
201+
}
202+
if ok && ans < t {
203+
ans = t
204+
}
205+
}
206+
return
207+
}
84208
```
85209

86210
### **...**

solution/1200-1299/1255.Maximum Score Words Formed by Letters/README_EN.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,129 @@ Letter &quot;e&quot; can only be used once.</pre>
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int:
64+
cnt = Counter(letters)
65+
n = len(words)
66+
ans = 0
67+
for i in range(1 << n):
68+
cur = Counter(''.join([words[j] for j in range(n) if i >> j & 1]))
69+
if all(v <= cnt[c] for c, v in cur.items()):
70+
t = sum(v * score[ord(c) - ord('a')] for c, v in cur.items())
71+
ans = max(ans, t)
72+
return ans
6373
```
6474

6575
### **Java**
6676

6777
```java
78+
class Solution {
79+
public int maxScoreWords(String[] words, char[] letters, int[] score) {
80+
int[] cnt = new int[26];
81+
for (int i = 0; i < letters.length; ++i) {
82+
cnt[letters[i] - 'a']++;
83+
}
84+
int n = words.length;
85+
int ans = 0;
86+
for (int i = 0; i < 1 << n; ++i) {
87+
int[] cur = new int[26];
88+
for (int j = 0; j < n; ++j) {
89+
if (((i >> j) & 1) == 1) {
90+
for (int k = 0; k < words[j].length(); ++k) {
91+
cur[words[j].charAt(k) - 'a']++;
92+
}
93+
}
94+
}
95+
boolean ok = true;
96+
int t = 0;
97+
for (int j = 0; j < 26; ++j) {
98+
if (cur[j] > cnt[j]) {
99+
ok = false;
100+
break;
101+
}
102+
t += cur[j] * score[j];
103+
}
104+
if (ok && ans < t) {
105+
ans = t;
106+
}
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
119+
int cnt[26]{};
120+
for (char& c : letters) {
121+
cnt[c - 'a']++;
122+
}
123+
int n = words.size();
124+
int ans = 0;
125+
for (int i = 0; i < 1 << n; ++i) {
126+
int cur[26]{};
127+
for (int j = 0; j < n; ++j) {
128+
if (i >> j & 1) {
129+
for (char& c : words[j]) {
130+
cur[c - 'a']++;
131+
}
132+
}
133+
}
134+
bool ok = true;
135+
int t = 0;
136+
for (int j = 0; j < 26; ++j) {
137+
if (cur[j] > cnt[j]) {
138+
ok = false;
139+
break;
140+
}
141+
t += cur[j] * score[j];
142+
}
143+
if (ok && ans < t) {
144+
ans = t;
145+
}
146+
}
147+
return ans;
148+
}
149+
};
150+
```
68151
152+
### **Go**
153+
154+
```go
155+
func maxScoreWords(words []string, letters []byte, score []int) (ans int) {
156+
cnt := [26]int{}
157+
for _, c := range letters {
158+
cnt[c-'a']++
159+
}
160+
n := len(words)
161+
for i := 0; i < 1<<n; i++ {
162+
cur := [26]int{}
163+
for j := 0; j < n; j++ {
164+
if i>>j&1 == 1 {
165+
for _, c := range words[j] {
166+
cur[c-'a']++
167+
}
168+
}
169+
}
170+
ok := true
171+
t := 0
172+
for i, v := range cur {
173+
if v > cnt[i] {
174+
ok = false
175+
break
176+
}
177+
t += v * score[i]
178+
}
179+
if ok && ans < t {
180+
ans = t
181+
}
182+
}
183+
return
184+
}
69185
```
70186

71187
### **...**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
4+
int cnt[26]{};
5+
for (char& c : letters) {
6+
cnt[c - 'a']++;
7+
}
8+
int n = words.size();
9+
int ans = 0;
10+
for (int i = 0; i < 1 << n; ++i) {
11+
int cur[26]{};
12+
for (int j = 0; j < n; ++j) {
13+
if (i >> j & 1) {
14+
for (char& c : words[j]) {
15+
cur[c - 'a']++;
16+
}
17+
}
18+
}
19+
bool ok = true;
20+
int t = 0;
21+
for (int j = 0; j < 26; ++j) {
22+
if (cur[j] > cnt[j]) {
23+
ok = false;
24+
break;
25+
}
26+
t += cur[j] * score[j];
27+
}
28+
if (ok && ans < t) {
29+
ans = t;
30+
}
31+
}
32+
return ans;
33+
}
34+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func maxScoreWords(words []string, letters []byte, score []int) (ans int) {
2+
cnt := [26]int{}
3+
for _, c := range letters {
4+
cnt[c-'a']++
5+
}
6+
n := len(words)
7+
for i := 0; i < 1<<n; i++ {
8+
cur := [26]int{}
9+
for j := 0; j < n; j++ {
10+
if i>>j&1 == 1 {
11+
for _, c := range words[j] {
12+
cur[c-'a']++
13+
}
14+
}
15+
}
16+
ok := true
17+
t := 0
18+
for i, v := range cur {
19+
if v > cnt[i] {
20+
ok = false
21+
break
22+
}
23+
t += v * score[i]
24+
}
25+
if ok && ans < t {
26+
ans = t
27+
}
28+
}
29+
return
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int maxScoreWords(String[] words, char[] letters, int[] score) {
3+
int[] cnt = new int[26];
4+
for (int i = 0; i < letters.length; ++i) {
5+
cnt[letters[i] - 'a']++;
6+
}
7+
int n = words.length;
8+
int ans = 0;
9+
for (int i = 0; i < 1 << n; ++i) {
10+
int[] cur = new int[26];
11+
for (int j = 0; j < n; ++j) {
12+
if (((i >> j) & 1) == 1) {
13+
for (int k = 0; k < words[j].length(); ++k) {
14+
cur[words[j].charAt(k) - 'a']++;
15+
}
16+
}
17+
}
18+
boolean ok = true;
19+
int t = 0;
20+
for (int j = 0; j < 26; ++j) {
21+
if (cur[j] > cnt[j]) {
22+
ok = false;
23+
break;
24+
}
25+
t += cur[j] * score[j];
26+
}
27+
if (ok && ans < t) {
28+
ans = t;
29+
}
30+
}
31+
return ans;
32+
}
33+
}

0 commit comments

Comments
 (0)