Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1967 (#3943)
Browse files Browse the repository at this point in the history
No.1967.Number of Strings That Appear as Substrings in Word
  • Loading branch information
yanglbme authored Jan 11, 2025
1 parent e27eeca commit 8264fe9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ tags:

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

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

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

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ Note that we only care about characters that are still in the string at the end

<!-- solution:start -->

### Solution 1
### Solution 1: Array + Sorting

First, we use an array $\textit{cnt}$ of length $26$ to count the occurrences of each letter in the string $s$.

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.

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.

After traversing, we return the answer.

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$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ patterns 中有 2 个字符串作为子字符串出现在 word 中。

### 方法一:模拟

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

遍历结束后,返回答案。

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

<!-- tabs:start -->

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

```ts
function numOfStrings(patterns: string[], word: string): number {
let ans = 0;
for (const p of patterns) {
if (word.includes(p)) {
++ans;
}
return patterns.filter(p => word.includes(p)).length;
}
```

#### Rust

```rust
impl Solution {
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
patterns.iter().filter(|p| word.contains(&**p)).count() as i32
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

After traversing, return the answer.

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.

<!-- tabs:start -->

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

```ts
function numOfStrings(patterns: string[], word: string): number {
let ans = 0;
for (const p of patterns) {
if (word.includes(p)) {
++ans;
}
return patterns.filter(p => word.includes(p)).length;
}
```

#### Rust

```rust
impl Solution {
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
patterns.iter().filter(|p| word.contains(&**p)).count() as i32
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
patterns.iter().filter(|p| word.contains(&**p)).count() as i32
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
function numOfStrings(patterns: string[], word: string): number {
let ans = 0;
for (const p of patterns) {
if (word.includes(p)) {
++ans;
}
}
return ans;
return patterns.filter(p => word.includes(p)).length;
}

0 comments on commit 8264fe9

Please sign in to comment.