diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md index ed23af50499ac..d1f36f1fa7748 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md @@ -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$。 diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md index 38e7ba8a18b9b..0969fb33e9ecb 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md @@ -67,7 +67,17 @@ Note that we only care about characters that are still in the string at the end -### 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$. diff --git a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README.md b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README.md index ae1a4fefd88f8..b31208b7d2369 100644 --- a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README.md +++ b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README.md @@ -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}$ 的长度。 @@ -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, word: String) -> i32 { + patterns.iter().filter(|p| word.contains(&**p)).count() as i32 } - return ans; } ``` diff --git a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README_EN.md b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README_EN.md index dcee15a4cf98a..aaa5163b50cef 100644 --- a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README_EN.md +++ b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README_EN.md @@ -73,7 +73,13 @@ tags: -### 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. @@ -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, word: String) -> i32 { + patterns.iter().filter(|p| word.contains(&**p)).count() as i32 } - return ans; } ``` diff --git a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.rs b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.rs new file mode 100644 index 0000000000000..f0736b3bc1b07 --- /dev/null +++ b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.rs @@ -0,0 +1,5 @@ +impl Solution { + pub fn num_of_strings(patterns: Vec, word: String) -> i32 { + patterns.iter().filter(|p| word.contains(&**p)).count() as i32 + } +} diff --git a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.ts b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.ts index 15bdb15c00f00..90b9430998b59 100644 --- a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.ts +++ b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.ts @@ -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; }