Skip to content

Commit b5d3751

Browse files
committed
+ problem 2135
1 parent 9b3440a commit b5d3751

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 2135. Count Words Obtained After Adding a Letter
2+
You are given two **0-indexed** arrays of strings `startWords` and `targetWords`. Each string consists of **lowercase English letters** only.
3+
4+
For each string in `targetWords`, check if it is possible to choose a string from `startWords` and perform a **conversion operation** on it to be equal to that from `targetWords`.
5+
6+
The **conversion operation** is described in the following two steps:
7+
8+
1. **Append** any lowercase letter that is **not present** in the string to its end.
9+
* For example, if the string is `"abc"`, the letters `'d'`, `'e'`, or `'y'` can be added to it, but not `'a'`. If `'d'` is added, the resulting string will be `"abcd"`.
10+
2. **Rearrange** the letters of the new string in **any** arbitrary order.
11+
* For example, `"abcd"` can be rearranged to `"acbd"`, `"bacd"`, `"cbda"`, and so on. Note that it can also be rearranged to `"abcd"` itself.
12+
13+
Return *the **number of strings** in* `targetWords` *that can be obtained by performing the operations on **any** string of* `startWords`.
14+
15+
**Note** that you will only be verifying if the string in `targetWords` can be obtained from a string in `startWords` by performing the operations. The strings in `startWords` **do not** actually change during this process.
16+
17+
#### Example 1:
18+
<pre>
19+
<strong>Input:</strong> startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"]
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong>
22+
- In order to form targetWords[0] = "tack", we use startWords[1] = "act", append 'k' to it, and rearrange "actk" to "tack".
23+
- There is no string in startWords that can be used to obtain targetWords[1] = "act".
24+
Note that "act" does exist in startWords, but we must append one letter to the string before rearranging it.
25+
- In order to form targetWords[2] = "acti", we use startWords[1] = "act", append 'i' to it, and rearrange "acti" to "acti" itself.
26+
</pre>
27+
28+
#### Example 2:
29+
<pre>
30+
<strong>Input:</strong> startWords = ["ab","a"], targetWords = ["abc","abcd"]
31+
<strong>Output:</strong> 1
32+
<strong>Explanation:</strong>
33+
- In order to form targetWords[0] = "abc", we use startWords[0] = "ab", add 'c' to it, and rearrange it to "abc".
34+
- There is no string in startWords that can be used to obtain targetWords[1] = "abcd".
35+
</pre>
36+
37+
#### Constraints:
38+
* <code>1 <= startWords.length, targetWords.length <= 5 * 10<sup>4</sup></code>
39+
* `1 <= startWords[i].length, targetWords[j].length <= 26`
40+
* Each string of `startWords` and `targetWords` consists of lowercase English letters only.
41+
* No letter occurs more than once in any string of `startWords` or `targetWords`.
42+
43+
## Solutions (Rust)
44+
45+
### 1. Solution
46+
```Rust
47+
use std::collections::HashMap;
48+
49+
impl Solution {
50+
pub fn word_count(start_words: Vec<String>, target_words: Vec<String>) -> i32 {
51+
let mut target_count = HashMap::new();
52+
let mut ret = 0;
53+
54+
for target in target_words {
55+
let mut count = [0; 26];
56+
57+
for ch in target.bytes() {
58+
count[(ch - b'a') as usize] += 1;
59+
}
60+
61+
*target_count.entry(count).or_insert(0) += 1;
62+
}
63+
64+
for start in start_words {
65+
let mut count = [0; 26];
66+
67+
for ch in start.bytes() {
68+
count[(ch - b'a') as usize] += 1;
69+
}
70+
71+
for i in 0..26 {
72+
if count[i] == 0 {
73+
let mut tmp = count;
74+
tmp[i] = 1;
75+
ret += target_count.remove(&tmp).unwrap_or(0);
76+
}
77+
}
78+
}
79+
80+
ret
81+
}
82+
}
83+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 2135. 统计追加字母可以获得的单词数
2+
给你两个下标从 **0** 开始的字符串数组 `startWords``targetWords` 。每个字符串都仅由 **小写英文字母** 组成。
3+
4+
对于 `targetWords` 中的每个字符串,检查是否能够从 `startWords` 中选出一个字符串,执行一次 **转换操作** ,得到的结果与当前 `targetWords` 字符串相等。
5+
6+
**转换操作** 如下面两步所述:
7+
8+
1. **追加** 任何 **不存在** 于当前字符串的任一小写字母到当前字符串的末尾。
9+
* 例如,如果字符串为 `"abc"` ,那么字母 `'d'``'e'``'y'` 都可以加到该字符串末尾,但 `'a'` 就不行。如果追加的是 `'d'` ,那么结果字符串为 `"abcd"`
10+
2. **重排** 新字符串中的字母,可以按 **任意** 顺序重新排布字母。
11+
* 例如,`"abcd"` 可以重排为 `"acbd"``"bacd"``"cbda"`,以此类推。注意,它也可以重排为 `"abcd"` 自身。
12+
13+
找出 `targetWords` 中有多少字符串能够由 `startWords` 中的 **任一** 字符串执行上述转换操作获得。返回 `targetWords` 中这类 **字符串的数目**
14+
15+
**注意:**你仅能验证 `targetWords` 中的字符串是否可以由 `startWords` 中的某个字符串经执行操作获得。`startWords` 中的字符串在这一过程中 **** 发生实际变更。
16+
17+
#### 示例 1:
18+
<pre>
19+
<strong>输入:</strong> startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"]
20+
<strong>输出:</strong> 2
21+
<strong>解释:</strong>
22+
- 为了形成 targetWords[0] = "tack" ,可以选用 startWords[1] = "act" ,追加字母 'k' ,并重排 "actk" 为 "tack" 。
23+
- startWords 中不存在可以用于获得 targetWords[1] = "act" 的字符串。
24+
注意 "act" 确实存在于 startWords ,但是 必须 在重排前给这个字符串追加一个字母。
25+
- 为了形成 targetWords[2] = "acti" ,可以选用 startWords[1] = "act" ,追加字母 'i' ,并重排 "acti" 为 "acti" 自身。
26+
</pre>
27+
28+
#### 示例 2:
29+
<pre>
30+
<strong>输入:</strong> startWords = ["ab","a"], targetWords = ["abc","abcd"]
31+
<strong>输出:</strong> 1
32+
<strong>解释:</strong>
33+
- 为了形成 targetWords[0] = "abc" ,可以选用 startWords[0] = "ab" ,追加字母 'c' ,并重排为 "abc" 。
34+
- startWords 中不存在可以用于获得 targetWords[1] = "abcd" 的字符串。
35+
</pre>
36+
37+
#### 提示:
38+
* <code>1 <= startWords.length, targetWords.length <= 5 * 10<sup>4</sup></code>
39+
* `1 <= startWords[i].length, targetWords[j].length <= 26`
40+
* `startWords``targetWords` 中的每个字符串都仅由小写英文字母组成
41+
*`startWords``targetWords` 的任一字符串中,每个字母至多出现一次
42+
43+
## 题解 (Rust)
44+
45+
### 1. 题解
46+
```Rust
47+
use std::collections::HashMap;
48+
49+
impl Solution {
50+
pub fn word_count(start_words: Vec<String>, target_words: Vec<String>) -> i32 {
51+
let mut target_count = HashMap::new();
52+
let mut ret = 0;
53+
54+
for target in target_words {
55+
let mut count = [0; 26];
56+
57+
for ch in target.bytes() {
58+
count[(ch - b'a') as usize] += 1;
59+
}
60+
61+
*target_count.entry(count).or_insert(0) += 1;
62+
}
63+
64+
for start in start_words {
65+
let mut count = [0; 26];
66+
67+
for ch in start.bytes() {
68+
count[(ch - b'a') as usize] += 1;
69+
}
70+
71+
for i in 0..26 {
72+
if count[i] == 0 {
73+
let mut tmp = count;
74+
tmp[i] = 1;
75+
ret += target_count.remove(&tmp).unwrap_or(0);
76+
}
77+
}
78+
}
79+
80+
ret
81+
}
82+
}
83+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn word_count(start_words: Vec<String>, target_words: Vec<String>) -> i32 {
5+
let mut target_count = HashMap::new();
6+
let mut ret = 0;
7+
8+
for target in target_words {
9+
let mut count = [0; 26];
10+
11+
for ch in target.bytes() {
12+
count[(ch - b'a') as usize] += 1;
13+
}
14+
15+
*target_count.entry(count).or_insert(0) += 1;
16+
}
17+
18+
for start in start_words {
19+
let mut count = [0; 26];
20+
21+
for ch in start.bytes() {
22+
count[(ch - b'a') as usize] += 1;
23+
}
24+
25+
for i in 0..26 {
26+
if count[i] == 0 {
27+
let mut tmp = count;
28+
tmp[i] = 1;
29+
ret += target_count.remove(&tmp).unwrap_or(0);
30+
}
31+
}
32+
}
33+
34+
ret
35+
}
36+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@
11161116
[2131][2131l]|[Longest Palindrome by Concatenating Two Letter Words][2131] |![rs]
11171117
[2133][2133l]|[Check if Every Row and Column Contains All Numbers][2133] |![rs]
11181118
[2134][2134l]|[Minimum Swaps to Group All 1's Together II][2134] |![rs]
1119+
[2135][2135l]|[Count Words Obtained After Adding a Letter][2135] |![rs]
11191120
[2138][2138l]|[Divide a String Into Groups of Size k][2138] |![py]
11201121
[2139][2139l]|[Minimum Moves to Reach Target Score][2139] |![rs]
11211122
[2140][2140l]|[Solving Questions With Brainpower][2140] |![rs]
@@ -2448,6 +2449,7 @@
24482449
[2131]:Problemset/2131-Longest%20Palindrome%20by%20Concatenating%20Two%20Letter%20Words/README.md#2131-longest-palindrome-by-concatenating-two-letter-words
24492450
[2133]:Problemset/2133-Check%20if%20Every%20Row%20and%20Column%20Contains%20All%20Numbers/README.md#2133-check-if-every-row-and-column-contains-all-numbers
24502451
[2134]:Problemset/2134-Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/README.md#2134-minimum-swaps-to-group-all-1s-together-ii
2452+
[2135]:Problemset/2135-Count%20Words%20Obtained%20After%20Adding%20a%20Letter/README.md#2135-count-words-obtained-after-adding-a-letter
24512453
[2138]:Problemset/2138-Divide%20a%20String%20Into%20Groups%20of%20Size%20k/README.md#2138-divide-a-string-into-groups-of-size-k
24522454
[2139]:Problemset/2139-Minimum%20Moves%20to%20Reach%20Target%20Score/README.md#2139-minimum-moves-to-reach-target-score
24532455
[2140]:Problemset/2140-Solving%20Questions%20With%20Brainpower/README.md#2140-solving-questions-with-brainpower
@@ -3783,6 +3785,7 @@
37833785
[2131l]:https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/
37843786
[2133l]:https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/
37853787
[2134l]:https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/
3788+
[2135l]:https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/
37863789
[2138l]:https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/
37873790
[2139l]:https://leetcode.com/problems/minimum-moves-to-reach-target-score/
37883791
[2140l]:https://leetcode.com/problems/solving-questions-with-brainpower/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@
11161116
[2131][2131l]|[连接两字母单词得到的最长回文串][2131] |![rs]
11171117
[2133][2133l]|[检查是否每一行每一列都包含全部整数][2133] |![rs]
11181118
[2134][2134l]|[最少交换次数来组合所有的 1 II][2134] |![rs]
1119+
[2135][2135l]|[统计追加字母可以获得的单词数][2135] |![rs]
11191120
[2138][2138l]|[将字符串拆分为若干长度为 k 的组][2138] |![py]
11201121
[2139][2139l]|[得到目标值的最少行动次数][2139] |![rs]
11211122
[2140][2140l]|[解决智力问题][2140] |![rs]
@@ -2448,6 +2449,7 @@
24482449
[2131]:Problemset/2131-Longest%20Palindrome%20by%20Concatenating%20Two%20Letter%20Words/README_CN.md#2131-连接两字母单词得到的最长回文串
24492450
[2133]:Problemset/2133-Check%20if%20Every%20Row%20and%20Column%20Contains%20All%20Numbers/README_CN.md#2133-检查是否每一行每一列都包含全部整数
24502451
[2134]:Problemset/2134-Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/README_CN.md#2134-最少交换次数来组合所有的-1-ii
2452+
[2135]:Problemset/2135-Count%20Words%20Obtained%20After%20Adding%20a%20Letter/README_CN.md#2135-统计追加字母可以获得的单词数
24512453
[2138]:Problemset/2138-Divide%20a%20String%20Into%20Groups%20of%20Size%20k/README_CN.md#2138-将字符串拆分为若干长度为-k-的组
24522454
[2139]:Problemset/2139-Minimum%20Moves%20to%20Reach%20Target%20Score/README_CN.md#2139-得到目标值的最少行动次数
24532455
[2140]:Problemset/2140-Solving%20Questions%20With%20Brainpower/README_CN.md#2140-解决智力问题
@@ -3783,6 +3785,7 @@
37833785
[2131l]:https://leetcode.cn/problems/longest-palindrome-by-concatenating-two-letter-words/
37843786
[2133l]:https://leetcode.cn/problems/check-if-every-row-and-column-contains-all-numbers/
37853787
[2134l]:https://leetcode.cn/problems/minimum-swaps-to-group-all-1s-together-ii/
3788+
[2135l]:https://leetcode.cn/problems/count-words-obtained-after-adding-a-letter/
37863789
[2138l]:https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/
37873790
[2139l]:https://leetcode.cn/problems/minimum-moves-to-reach-target-score/
37883791
[2140l]:https://leetcode.cn/problems/solving-questions-with-brainpower/

0 commit comments

Comments
 (0)