Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dbb90fc

Browse files
committed
feat: add solutions to lc problem: No.1160
No.1160.Find Words That Can Be Formed by Characters
1 parent 3d25f7d commit dbb90fc

File tree

6 files changed

+294
-23
lines changed

6 files changed

+294
-23
lines changed

‎solution/1100-1199/1160.Find Words That Can Be Formed by Characters/README.md‎

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
<li>所有字符串中都仅包含小写英文字母</li>
4343
</ol>
4444

45-
4645
## 解法
4746

4847
<!-- 这里可写通用的实现逻辑 -->
@@ -54,15 +53,115 @@
5453
<!-- 这里可写当前语言的特殊实现逻辑 -->
5554

5655
```python
57-
56+
class Solution:
57+
def countCharacters(self, words: List[str], chars: str) -> int:
58+
counter = Counter(chars)
59+
ans = 0
60+
for word in words:
61+
cnt = Counter(word)
62+
if all([counter[c] >= v for c, v in cnt.items()]):
63+
ans += len(word)
64+
return ans
5865
```
5966

6067
### **Java**
6168

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

6471
```java
72+
class Solution {
73+
public int countCharacters(String[] words, String chars) {
74+
int[] counter = count(chars);
75+
int ans = 0;
76+
for (String word : words) {
77+
int[] cnt = count(word);
78+
if (check(counter, cnt)) {
79+
ans += word.length();
80+
}
81+
}
82+
return ans;
83+
}
84+
85+
private int[] count(String s) {
86+
int[] counter = new int[26];
87+
for (char c : s.toCharArray()) {
88+
++counter[c - 'a'];
89+
}
90+
return counter;
91+
}
92+
93+
private boolean check(int[] cnt1, int[] cnt2) {
94+
for (int i = 0; i < 26; ++i) {
95+
if (cnt1[i] < cnt2[i]) {
96+
return false;
97+
}
98+
}
99+
return true;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int countCharacters(vector<string>& words, string chars) {
110+
vector<int> counter = count(chars);
111+
int ans = 0;
112+
for (auto& word : words)
113+
{
114+
vector<int> cnt = count(word);
115+
if (check(counter, cnt)) ans += word.size();
116+
}
117+
return ans;
118+
}
119+
120+
vector<int> count(string s) {
121+
vector<int> counter(26);
122+
for (char c : s) ++counter[c - 'a'];
123+
return counter;
124+
}
125+
126+
bool check(vector<int>& cnt1, vector<int>& cnt2) {
127+
for (int i = 0; i < 26; ++i)
128+
if (cnt1[i] < cnt2[i]) return false;
129+
return true;
130+
}
131+
};
132+
```
65133
134+
### **Go**
135+
136+
```go
137+
func countCharacters(words []string, chars string) int {
138+
counter := count(chars)
139+
ans := 0
140+
for _, word := range words {
141+
cnt := count(word)
142+
if check(counter, cnt) {
143+
ans += len(word)
144+
}
145+
}
146+
return ans
147+
}
148+
149+
func count(s string) []int {
150+
counter := make([]int, 26)
151+
for _, c := range s {
152+
counter[c-'a']++
153+
}
154+
return counter
155+
}
156+
157+
func check(cnt1, cnt2 []int) bool {
158+
for i := 0; i < 26; i++ {
159+
if cnt1[i] < cnt2[i] {
160+
return false
161+
}
162+
}
163+
return true
164+
}
66165
```
67166

68167
### **...**

‎solution/1100-1199/1160.Find Words That Can Be Formed by Characters/README_EN.md‎

Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@
66

77
<p>You are given an array of strings&nbsp;<code>words</code>&nbsp;and a string&nbsp;<code>chars</code>.</p>
88

9-
10-
119
<p>A string is <em>good</em>&nbsp;if&nbsp;it can be formed by&nbsp;characters from <code>chars</code>&nbsp;(each character&nbsp;can only be used once).</p>
1210

13-
14-
1511
<p>Return the sum of lengths of all good strings in <code>words</code>.</p>
1612

17-
18-
1913
<p>&nbsp;</p>
2014

21-
22-
2315
<p><strong>Example 1:</strong></p>
2416

25-
26-
2717
<pre>
2818

2919
<strong>Input: </strong>words = <span id="example-input-1-1">[&quot;cat&quot;,&quot;bt&quot;,&quot;hat&quot;,&quot;tree&quot;]</span>, chars = <span id="example-input-1-2">&quot;atach&quot;</span>
@@ -36,12 +26,8 @@ The strings that can be formed are &quot;cat&quot; and &quot;hat&quot; so the an
3626

3727
</pre>
3828

39-
40-
4129
<p><strong>Example 2:</strong></p>
4230

43-
44-
4531
<pre>
4632

4733
<strong>Input: </strong>words = <span id="example-input-2-1">[&quot;hello&quot;,&quot;world&quot;,&quot;leetcode&quot;]</span>, chars = <span id="example-input-2-2">&quot;welldonehoneyr&quot;</span>
@@ -54,16 +40,10 @@ The strings that can be formed are &quot;hello&quot; and &quot;world&quot; so th
5440

5541
</pre>
5642

57-
58-
5943
<p>&nbsp;</p>
6044

61-
62-
6345
<p><span><strong>Note:</strong></span></p>
6446

65-
66-
6747
<ol>
6848
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
6949
<li><code>1 &lt;= words[i].length, chars.length&nbsp;&lt;= 100</code></li>
@@ -77,13 +57,113 @@ The strings that can be formed are &quot;hello&quot; and &quot;world&quot; so th
7757
### **Python3**
7858

7959
```python
80-
60+
class Solution:
61+
def countCharacters(self, words: List[str], chars: str) -> int:
62+
counter = Counter(chars)
63+
ans = 0
64+
for word in words:
65+
cnt = Counter(word)
66+
if all([counter[c] >= v for c, v in cnt.items()]):
67+
ans += len(word)
68+
return ans
8169
```
8270

8371
### **Java**
8472

8573
```java
74+
class Solution {
75+
public int countCharacters(String[] words, String chars) {
76+
int[] counter = count(chars);
77+
int ans = 0;
78+
for (String word : words) {
79+
int[] cnt = count(word);
80+
if (check(counter, cnt)) {
81+
ans += word.length();
82+
}
83+
}
84+
return ans;
85+
}
86+
87+
private int[] count(String s) {
88+
int[] counter = new int[26];
89+
for (char c : s.toCharArray()) {
90+
++counter[c - 'a'];
91+
}
92+
return counter;
93+
}
94+
95+
private boolean check(int[] cnt1, int[] cnt2) {
96+
for (int i = 0; i < 26; ++i) {
97+
if (cnt1[i] < cnt2[i]) {
98+
return false;
99+
}
100+
}
101+
return true;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int countCharacters(vector<string>& words, string chars) {
112+
vector<int> counter = count(chars);
113+
int ans = 0;
114+
for (auto& word : words)
115+
{
116+
vector<int> cnt = count(word);
117+
if (check(counter, cnt)) ans += word.size();
118+
}
119+
return ans;
120+
}
121+
122+
vector<int> count(string s) {
123+
vector<int> counter(26);
124+
for (char c : s) ++counter[c - 'a'];
125+
return counter;
126+
}
127+
128+
bool check(vector<int>& cnt1, vector<int>& cnt2) {
129+
for (int i = 0; i < 26; ++i)
130+
if (cnt1[i] < cnt2[i]) return false;
131+
return true;
132+
}
133+
};
134+
```
86135
136+
### **Go**
137+
138+
```go
139+
func countCharacters(words []string, chars string) int {
140+
counter := count(chars)
141+
ans := 0
142+
for _, word := range words {
143+
cnt := count(word)
144+
if check(counter, cnt) {
145+
ans += len(word)
146+
}
147+
}
148+
return ans
149+
}
150+
151+
func count(s string) []int {
152+
counter := make([]int, 26)
153+
for _, c := range s {
154+
counter[c-'a']++
155+
}
156+
return counter
157+
}
158+
159+
func check(cnt1, cnt2 []int) bool {
160+
for i := 0; i < 26; i++ {
161+
if cnt1[i] < cnt2[i] {
162+
return false
163+
}
164+
}
165+
return true
166+
}
87167
```
88168

89169
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int countCharacters(vector<string>& words, string chars) {
4+
vector<int> counter = count(chars);
5+
int ans = 0;
6+
for (auto& word : words)
7+
{
8+
vector<int> cnt = count(word);
9+
if (check(counter, cnt)) ans += word.size();
10+
}
11+
return ans;
12+
}
13+
14+
vector<int> count(string s) {
15+
vector<int> counter(26);
16+
for (char c : s) ++counter[c - 'a'];
17+
return counter;
18+
}
19+
20+
bool check(vector<int>& cnt1, vector<int>& cnt2) {
21+
for (int i = 0; i < 26; ++i)
22+
if (cnt1[i] < cnt2[i]) return false;
23+
return true;
24+
}
25+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func countCharacters(words []string, chars string) int {
2+
counter := count(chars)
3+
ans := 0
4+
for _, word := range words {
5+
cnt := count(word)
6+
if check(counter, cnt) {
7+
ans += len(word)
8+
}
9+
}
10+
return ans
11+
}
12+
13+
func count(s string) []int {
14+
counter := make([]int, 26)
15+
for _, c := range s {
16+
counter[c-'a']++
17+
}
18+
return counter
19+
}
20+
21+
func check(cnt1, cnt2 []int) bool {
22+
for i := 0; i < 26; i++ {
23+
if cnt1[i] < cnt2[i] {
24+
return false
25+
}
26+
}
27+
return true
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int countCharacters(String[] words, String chars) {
3+
int[] counter = count(chars);
4+
int ans = 0;
5+
for (String word : words) {
6+
int[] cnt = count(word);
7+
if (check(counter, cnt)) {
8+
ans += word.length();
9+
}
10+
}
11+
return ans;
12+
}
13+
14+
private int[] count(String s) {
15+
int[] counter = new int[26];
16+
for (char c : s.toCharArray()) {
17+
++counter[c - 'a'];
18+
}
19+
return counter;
20+
}
21+
22+
private boolean check(int[] cnt1, int[] cnt2) {
23+
for (int i = 0; i < 26; ++i) {
24+
if (cnt1[i] < cnt2[i]) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def countCharacters(self, words: List[str], chars: str) -> int:
3+
counter = Counter(chars)
4+
ans = 0
5+
for word in words:
6+
cnt = Counter(word)
7+
if all([counter[c] >= v for c, v in cnt.items()]):
8+
ans += len(word)
9+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /