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 8264fe9

Browse files
feat: add solutions to lc problem: No.1967 (doocs#3943)
No.1967.Number of Strings That Appear as Substrings in Word
1 parent e27eeca commit 8264fe9

File tree

6 files changed

+50
-27
lines changed

6 files changed

+50
-27
lines changed

‎solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ tags:
7171

7272
### 方法一:数组 + 排序
7373

74-
我们先用一个长度为 26ドル$ 的数组 `cnt` 统计字符串 $s$ 中每个字母出现的次数。
74+
我们先用一个长度为 26ドル$ 的数组 $\textit{cnt}$ 统计字符串 $s$ 中每个字母出现的次数。
7575

76-
然后我们对数组 `cnt` 进行倒序排序。定义一个变量 `pre` 记录当前字母的出现次数。
76+
然后我们对数组 $\textit{cnt}$ 进行倒序排序。定义一个变量 $\textit{pre}$ 记录当前字母的出现次数。
7777

78-
接下来,遍历数组 `cnt` 每个元素 $v,ドル如果当前 `pre` 等于 0ドル,ドル我们直接将答案加上 $v$;否则,如果 $v \geq pre,ドル我们将答案加上 $v-pre+1,ドル并且将 `pre` 减去 1ドル,ドル否则,我们直接将 `pre` 更新为 $v$。然后继续遍历下个元素。
78+
接下来,遍历数组 $\textit{cnt}$ 每个元素 $v,ドル如果当前 $\textit{pre}$ 等于 0ドル,ドル我们直接将答案加上 $v$;否则,如果 $v \geq \textit{pre},ドル我们将答案加上 $v-\textit{pre}+1,ドル并且将 $\textit{pre}$ 减去 1ドル,ドル否则,我们直接将 $\textit{pre}$ 更新为 $v$。然后继续遍历下个元素。
7979

8080
遍历结束,返回答案即可。
8181

82-
时间复杂度 $O(n + C \times \log C),ドル空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度,而 $C$ 为字母集的大小。本题中 $C=26$。
82+
时间复杂度 $O(n + |\Sigma| \times \log |\Sigma|),ドル空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $s$ 的长度,而 $|\Sigma|$ 为字母集的大小。本题中 $|\Sigma|=26$。
8383

8484
<!-- tabs:start -->
8585

‎solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ Note that we only care about characters that are still in the string at the end
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Array + Sorting
71+
72+
First, we use an array $\textit{cnt}$ of length 26ドル$ to count the occurrences of each letter in the string $s$.
73+
74+
Then, we sort the array $\textit{cnt}$ in descending order. We define a variable $\textit{pre}$ to record the current number of occurrences of the letter.
75+
76+
Next, we traverse each element $v$ in the array $\textit{cnt}$. If the current $\textit{pre}$ is 0ドル,ドル we directly add $v$ to the answer. Otherwise, if $v \geq \textit{pre},ドル we add $v - \textit{pre} + 1$ to the answer and decrement $\textit{pre}$ by 1ドル$. Otherwise, we directly update $\textit{pre}$ to $v$. Then, we continue to the next element.
77+
78+
After traversing, we return the answer.
79+
80+
The time complexity is $O(n + |\Sigma| \times \log |\Sigma|),ドル and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string $s,ドル and $|\Sigma|$ is the size of the alphabet. In this problem, $|\Sigma| = 26$.
7181

7282
<!-- tabs:start -->
7383

‎solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README.md‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ patterns 中有 2 个字符串作为子字符串出现在 word 中。
7777

7878
### 方法一:模拟
7979

80-
遍历字符串数组 $patterns$ 中的每个字符串 $p,ドル判断其是否为 $word$ 的子字符串,如果是,答案加一。
80+
遍历字符串数组 $\textit{patterns}$ 中的每个字符串 $p,ドル判断其是否为 $\textit{word}$ 的子字符串,如果是,答案加一。
8181

8282
遍历结束后,返回答案。
8383

84-
时间复杂度 $O(n \times m),ドル空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为 $patterns$ 和 $word$ 的长度。
84+
时间复杂度 $O(n \times m),ドル空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为 $\textit{patterns}$ 和 $\textit{word}$ 的长度。
8585

8686
<!-- tabs:start -->
8787

@@ -141,13 +141,17 @@ func numOfStrings(patterns []string, word string) (ans int) {
141141

142142
```ts
143143
function numOfStrings(patterns: string[], word: string): number {
144-
let ans = 0;
145-
for (const p of patterns) {
146-
if (word.includes(p)) {
147-
++ans;
148-
}
144+
return patterns.filter(p => word.includes(p)).length;
145+
}
146+
```
147+
148+
#### Rust
149+
150+
```rust
151+
impl Solution {
152+
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
153+
patterns.iter().filter(|p| word.contains(&**p)).count() as i32
149154
}
150-
return ans;
151155
}
152156
```
153157

‎solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README_EN.md‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ tags:
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Simulation
77+
78+
Traverse each string $p$ in the array $\textit{patterns}$ and check if it is a substring of $\textit{word}$. If it is, increment the answer by one.
79+
80+
After traversing, return the answer.
81+
82+
The time complexity is $O(n \times m),ドル and the space complexity is $O(1)$. Here, $n$ and $m$ are the lengths of $\textit{patterns}$ and $\textit{word},ドル respectively.
7783

7884
<!-- tabs:start -->
7985

@@ -133,13 +139,17 @@ func numOfStrings(patterns []string, word string) (ans int) {
133139

134140
```ts
135141
function numOfStrings(patterns: string[], word: string): number {
136-
let ans = 0;
137-
for (const p of patterns) {
138-
if (word.includes(p)) {
139-
++ans;
140-
}
142+
return patterns.filter(p => word.includes(p)).length;
143+
}
144+
```
145+
146+
#### Rust
147+
148+
```rust
149+
impl Solution {
150+
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
151+
patterns.iter().filter(|p| word.contains(&**p)).count() as i32
141152
}
142-
return ans;
143153
}
144154
```
145155

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
3+
patterns.iter().filter(|p| word.contains(&**p)).count() as i32
4+
}
5+
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
function numOfStrings(patterns: string[], word: string): number {
2-
let ans = 0;
3-
for (const p of patterns) {
4-
if (word.includes(p)) {
5-
++ans;
6-
}
7-
}
8-
return ans;
2+
return patterns.filter(p => word.includes(p)).length;
93
}

0 commit comments

Comments
(0)

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