diff --git a/solution/2200-2299/2255.Count Prefixes of a Given String/README.md b/solution/2200-2299/2255.Count Prefixes of a Given String/README.md index 279f9f900b29c..2dae1afbb8b5d 100644 --- a/solution/2200-2299/2255.Count Prefixes of a Given String/README.md +++ b/solution/2200-2299/2255.Count Prefixes of a Given String/README.md @@ -62,11 +62,11 @@ words 中是 s = "abc" 前缀的字符串为: ### 方法一:遍历计数 -我们直接遍历数组 $words$,对于每个字符串 $w$,判断 $s$ 是否以 $w$ 为前缀,如果是则答案加一。 +我们直接遍历数组 $\textit{words}$,对于每个字符串 $w$,判断 $s$ 是否以 $w$ 为前缀,如果是则答案加一。 遍历结束后,返回答案即可。 -时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是数组 $words$ 的长度和字符串 $s$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是数组 $\textit{words}$ 的长度和字符串 $s$ 的长度。空间复杂度 $O(1)$。 @@ -130,6 +130,26 @@ function countPrefixes(words: string[], s: string): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_prefixes(words: Vec, s: String) -> i32 { + words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32 + } +} +``` + +#### C# + +```cs +public class Solution { + public int CountPrefixes(string[] words, string s) { + return words.Count(w => s.StartsWith(w)); + } +} +``` + diff --git a/solution/2200-2299/2255.Count Prefixes of a Given String/README_EN.md b/solution/2200-2299/2255.Count Prefixes of a Given String/README_EN.md index 5db0aa3641db0..7c9c5be0f3ed5 100644 --- a/solution/2200-2299/2255.Count Prefixes of a Given String/README_EN.md +++ b/solution/2200-2299/2255.Count Prefixes of a Given String/README_EN.md @@ -42,7 +42,7 @@ Thus the number of strings in words which are a prefix of s is 3. Input: words = ["a","a"], s = "aa" Output: 2 Explanation: -Both of the strings are a prefix of s. +Both of the strings are a prefix of s. Note that the same string can occur multiple times in words, and it should be counted each time.

 

@@ -130,6 +130,26 @@ function countPrefixes(words: string[], s: string): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_prefixes(words: Vec, s: String) -> i32 { + words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32 + } +} +``` + +#### C# + +```cs +public class Solution { + public int CountPrefixes(string[] words, string s) { + return words.Count(w => s.StartsWith(w)); + } +} +``` + diff --git a/solution/2200-2299/2255.Count Prefixes of a Given String/Solution.cs b/solution/2200-2299/2255.Count Prefixes of a Given String/Solution.cs new file mode 100644 index 0000000000000..eefba51483213 --- /dev/null +++ b/solution/2200-2299/2255.Count Prefixes of a Given String/Solution.cs @@ -0,0 +1,5 @@ +public class Solution { + public int CountPrefixes(string[] words, string s) { + return words.Count(w => s.StartsWith(w)); + } +} diff --git a/solution/2200-2299/2255.Count Prefixes of a Given String/Solution.rs b/solution/2200-2299/2255.Count Prefixes of a Given String/Solution.rs new file mode 100644 index 0000000000000..c015f10a9f338 --- /dev/null +++ b/solution/2200-2299/2255.Count Prefixes of a Given String/Solution.rs @@ -0,0 +1,5 @@ +impl Solution { + pub fn count_prefixes(words: Vec, s: String) -> i32 { + words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32 + } +}