Skip to content

feat: add solutions to lc problem: No.3305 #4153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,50 @@ function countOfSubstrings(word: string, k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_of_substrings(word: String, k: i32) -> i32 {
fn f(word: &Vec<char>, k: i32) -> i32 {
let mut ans = 0;
let mut l = 0;
let mut x = 0;
let mut cnt = std::collections::HashMap::new();

let is_vowel = |c: char| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u');

for (r, &c) in word.iter().enumerate() {
if is_vowel(c) {
*cnt.entry(c).or_insert(0) += 1;
} else {
x += 1;
}

while x >= k && cnt.len() == 5 {
let d = word[l];
l += 1;
if is_vowel(d) {
let count = cnt.entry(d).or_insert(0);
*count -= 1;
if *count == 0 {
cnt.remove(&d);
}
} else {
x -= 1;
}
}
ans += l as i32;
}
ans
}

let chars: Vec<char> = word.chars().collect();
f(&chars, k) - f(&chars, k + 1)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,50 @@ function countOfSubstrings(word: string, k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_of_substrings(word: String, k: i32) -> i32 {
fn f(word: &Vec<char>, k: i32) -> i32 {
let mut ans = 0;
let mut l = 0;
let mut x = 0;
let mut cnt = std::collections::HashMap::new();

let is_vowel = |c: char| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u');

for (r, &c) in word.iter().enumerate() {
if is_vowel(c) {
*cnt.entry(c).or_insert(0) += 1;
} else {
x += 1;
}

while x >= k && cnt.len() == 5 {
let d = word[l];
l += 1;
if is_vowel(d) {
let count = cnt.entry(d).or_insert(0);
*count -= 1;
if *count == 0 {
cnt.remove(&d);
}
} else {
x -= 1;
}
}
ans += l as i32;
}
ans
}

let chars: Vec<char> = word.chars().collect();
f(&chars, k) - f(&chars, k + 1)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def countOfSubstrings(self, word: str, k: int) -> int:
def f(k: int) -> int:
cnt = Counter()
ans = l = x = 0
for c in word:
if c in "aeiou":
cnt[c] += 1
else:
x += 1
while x >= k and len(cnt) == 5:
d = word[l]
if d in "aeiou":
cnt[d] -= 1
if cnt[d] == 0:
cnt.pop(d)
else:
x -= 1
l += 1
ans += l
return ans

return f(k) - f(k + 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
impl Solution {
pub fn count_of_substrings(word: String, k: i32) -> i32 {
fn f(word: &Vec<char>, k: i32) -> i32 {
let mut ans = 0;
let mut l = 0;
let mut x = 0;
let mut cnt = std::collections::HashMap::new();

let is_vowel = |c: char| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u');

for (r, &c) in word.iter().enumerate() {
if is_vowel(c) {
*cnt.entry(c).or_insert(0) += 1;
} else {
x += 1;
}

while x >= k && cnt.len() == 5 {
let d = word[l];
l += 1;
if is_vowel(d) {
let count = cnt.entry(d).or_insert(0);
*count -= 1;
if *count == 0 {
cnt.remove(&d);
}
} else {
x -= 1;
}
}
ans += l as i32;
}
ans
}

let chars: Vec<char> = word.chars().collect();
f(&chars, k) - f(&chars, k + 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,50 @@ function countOfSubstrings(word: string, k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_of_substrings(word: String, k: i32) -> i64 {
fn f(word: &Vec<char>, k: i32) -> i64 {
let mut ans = 0_i64;
let mut l = 0;
let mut x = 0;
let mut cnt = std::collections::HashMap::new();

let is_vowel = |c: char| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u');

for (r, &c) in word.iter().enumerate() {
if is_vowel(c) {
*cnt.entry(c).or_insert(0) += 1;
} else {
x += 1;
}

while x >= k && cnt.len() == 5 {
let d = word[l];
l += 1;
if is_vowel(d) {
let count = cnt.entry(d).or_insert(0);
*count -= 1;
if *count == 0 {
cnt.remove(&d);
}
} else {
x -= 1;
}
}
ans += l as i64;
}
ans
}

let chars: Vec<char> = word.chars().collect();
f(&chars, k) - f(&chars, k + 1)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,50 @@ function countOfSubstrings(word: string, k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_of_substrings(word: String, k: i32) -> i64 {
fn f(word: &Vec<char>, k: i32) -> i64 {
let mut ans = 0_i64;
let mut l = 0;
let mut x = 0;
let mut cnt = std::collections::HashMap::new();

let is_vowel = |c: char| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u');

for (r, &c) in word.iter().enumerate() {
if is_vowel(c) {
*cnt.entry(c).or_insert(0) += 1;
} else {
x += 1;
}

while x >= k && cnt.len() == 5 {
let d = word[l];
l += 1;
if is_vowel(d) {
let count = cnt.entry(d).or_insert(0);
*count -= 1;
if *count == 0 {
cnt.remove(&d);
}
} else {
x -= 1;
}
}
ans += l as i64;
}
ans
}

let chars: Vec<char> = word.chars().collect();
f(&chars, k) - f(&chars, k + 1)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
impl Solution {
pub fn count_of_substrings(word: String, k: i32) -> i64 {
fn f(word: &Vec<char>, k: i32) -> i64 {
let mut ans = 0_i64;
let mut l = 0;
let mut x = 0;
let mut cnt = std::collections::HashMap::new();

let is_vowel = |c: char| matches!(c, 'a' | 'e' | 'i' | 'o' | 'u');

for (r, &c) in word.iter().enumerate() {
if is_vowel(c) {
*cnt.entry(c).or_insert(0) += 1;
} else {
x += 1;
}

while x >= k && cnt.len() == 5 {
let d = word[l];
l += 1;
if is_vowel(d) {
let count = cnt.entry(d).or_insert(0);
*count -= 1;
if *count == 0 {
cnt.remove(&d);
}
} else {
x -= 1;
}
}
ans += l as i64;
}
ans
}

let chars: Vec<char> = word.chars().collect();
f(&chars, k) - f(&chars, k + 1)
}
}
Loading