File tree Expand file tree Collapse file tree 7 files changed +277
-0
lines changed
3305.Count of Substrings Containing Every Vowel and K Consonants I
3306.Count of Substrings Containing Every Vowel and K Consonants II Expand file tree Collapse file tree 7 files changed +277
-0
lines changed Original file line number Diff line number Diff line change @@ -291,6 +291,50 @@ function countOfSubstrings(word: string, k: number): number {
291291}
292292```
293293
294+ #### Rust
295+
296+ ``` rust
297+ impl Solution {
298+ pub fn count_of_substrings (word : String , k : i32 ) -> i32 {
299+ fn f (word : & Vec <char >, k : i32 ) -> i32 {
300+ let mut ans = 0 ;
301+ let mut l = 0 ;
302+ let mut x = 0 ;
303+ let mut cnt = std :: collections :: HashMap :: new ();
304+
305+ let is_vowel = | c : char | matches! (c , 'a' | 'e' | 'i' | 'o' | 'u' );
306+
307+ for (r , & c ) in word . iter (). enumerate () {
308+ if is_vowel (c ) {
309+ * cnt . entry (c ). or_insert (0 ) += 1 ;
310+ } else {
311+ x += 1 ;
312+ }
313+
314+ while x >= k && cnt . len () == 5 {
315+ let d = word [l ];
316+ l += 1 ;
317+ if is_vowel (d ) {
318+ let count = cnt . entry (d ). or_insert (0 );
319+ * count -= 1 ;
320+ if * count == 0 {
321+ cnt . remove (& d );
322+ }
323+ } else {
324+ x -= 1 ;
325+ }
326+ }
327+ ans += l as i32 ;
328+ }
329+ ans
330+ }
331+
332+ let chars : Vec <char > = word . chars (). collect ();
333+ f (& chars , k ) - f (& chars , k + 1 )
334+ }
335+ }
336+ ```
337+
294338<!-- tabs:end -->
295339
296340<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -289,6 +289,50 @@ function countOfSubstrings(word: string, k: number): number {
289289}
290290```
291291
292+ #### Rust
293+
294+ ``` rust
295+ impl Solution {
296+ pub fn count_of_substrings (word : String , k : i32 ) -> i32 {
297+ fn f (word : & Vec <char >, k : i32 ) -> i32 {
298+ let mut ans = 0 ;
299+ let mut l = 0 ;
300+ let mut x = 0 ;
301+ let mut cnt = std :: collections :: HashMap :: new ();
302+
303+ let is_vowel = | c : char | matches! (c , 'a' | 'e' | 'i' | 'o' | 'u' );
304+
305+ for (r , & c ) in word . iter (). enumerate () {
306+ if is_vowel (c ) {
307+ * cnt . entry (c ). or_insert (0 ) += 1 ;
308+ } else {
309+ x += 1 ;
310+ }
311+
312+ while x >= k && cnt . len () == 5 {
313+ let d = word [l ];
314+ l += 1 ;
315+ if is_vowel (d ) {
316+ let count = cnt . entry (d ). or_insert (0 );
317+ * count -= 1 ;
318+ if * count == 0 {
319+ cnt . remove (& d );
320+ }
321+ } else {
322+ x -= 1 ;
323+ }
324+ }
325+ ans += l as i32 ;
326+ }
327+ ans
328+ }
329+
330+ let chars : Vec <char > = word . chars (). collect ();
331+ f (& chars , k ) - f (& chars , k + 1 )
332+ }
333+ }
334+ ```
335+
292336<!-- tabs:end -->
293337
294338<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def countOfSubstrings (self , word : str , k : int ) -> int :
3+ def f (k : int ) -> int :
4+ cnt = Counter ()
5+ ans = l = x = 0
6+ for c in word :
7+ if c in "aeiou" :
8+ cnt [c ] += 1
9+ else :
10+ x += 1
11+ while x >= k and len (cnt ) == 5 :
12+ d = word [l ]
13+ if d in "aeiou" :
14+ cnt [d ] -= 1
15+ if cnt [d ] == 0 :
16+ cnt .pop (d )
17+ else :
18+ x -= 1
19+ l += 1
20+ ans += l
21+ return ans
22+
23+ return f (k ) - f (k + 1 )
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn count_of_substrings ( word : String , k : i32 ) -> i32 {
3+ fn f ( word : & Vec < char > , k : i32 ) -> i32 {
4+ let mut ans = 0 ;
5+ let mut l = 0 ;
6+ let mut x = 0 ;
7+ let mut cnt = std:: collections:: HashMap :: new ( ) ;
8+
9+ let is_vowel = |c : char | matches ! ( c, 'a' | 'e' | 'i' | 'o' | 'u' ) ;
10+
11+ for ( r, & c) in word. iter ( ) . enumerate ( ) {
12+ if is_vowel ( c) {
13+ * cnt. entry ( c) . or_insert ( 0 ) += 1 ;
14+ } else {
15+ x += 1 ;
16+ }
17+
18+ while x >= k && cnt. len ( ) == 5 {
19+ let d = word[ l] ;
20+ l += 1 ;
21+ if is_vowel ( d) {
22+ let count = cnt. entry ( d) . or_insert ( 0 ) ;
23+ * count -= 1 ;
24+ if * count == 0 {
25+ cnt. remove ( & d) ;
26+ }
27+ } else {
28+ x -= 1 ;
29+ }
30+ }
31+ ans += l as i32 ;
32+ }
33+ ans
34+ }
35+
36+ let chars: Vec < char > = word. chars ( ) . collect ( ) ;
37+ f ( & chars, k) - f ( & chars, k + 1 )
38+ }
39+ }
Original file line number Diff line number Diff line change @@ -292,6 +292,50 @@ function countOfSubstrings(word: string, k: number): number {
292292}
293293```
294294
295+ #### Rust
296+
297+ ``` rust
298+ impl Solution {
299+ pub fn count_of_substrings (word : String , k : i32 ) -> i64 {
300+ fn f (word : & Vec <char >, k : i32 ) -> i64 {
301+ let mut ans = 0_i64 ;
302+ let mut l = 0 ;
303+ let mut x = 0 ;
304+ let mut cnt = std :: collections :: HashMap :: new ();
305+
306+ let is_vowel = | c : char | matches! (c , 'a' | 'e' | 'i' | 'o' | 'u' );
307+
308+ for (r , & c ) in word . iter (). enumerate () {
309+ if is_vowel (c ) {
310+ * cnt . entry (c ). or_insert (0 ) += 1 ;
311+ } else {
312+ x += 1 ;
313+ }
314+
315+ while x >= k && cnt . len () == 5 {
316+ let d = word [l ];
317+ l += 1 ;
318+ if is_vowel (d ) {
319+ let count = cnt . entry (d ). or_insert (0 );
320+ * count -= 1 ;
321+ if * count == 0 {
322+ cnt . remove (& d );
323+ }
324+ } else {
325+ x -= 1 ;
326+ }
327+ }
328+ ans += l as i64 ;
329+ }
330+ ans
331+ }
332+
333+ let chars : Vec <char > = word . chars (). collect ();
334+ f (& chars , k ) - f (& chars , k + 1 )
335+ }
336+ }
337+ ```
338+
295339<!-- tabs:end -->
296340
297341<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -289,6 +289,50 @@ function countOfSubstrings(word: string, k: number): number {
289289}
290290```
291291
292+ #### Rust
293+
294+ ``` rust
295+ impl Solution {
296+ pub fn count_of_substrings (word : String , k : i32 ) -> i64 {
297+ fn f (word : & Vec <char >, k : i32 ) -> i64 {
298+ let mut ans = 0_i64 ;
299+ let mut l = 0 ;
300+ let mut x = 0 ;
301+ let mut cnt = std :: collections :: HashMap :: new ();
302+
303+ let is_vowel = | c : char | matches! (c , 'a' | 'e' | 'i' | 'o' | 'u' );
304+
305+ for (r , & c ) in word . iter (). enumerate () {
306+ if is_vowel (c ) {
307+ * cnt . entry (c ). or_insert (0 ) += 1 ;
308+ } else {
309+ x += 1 ;
310+ }
311+
312+ while x >= k && cnt . len () == 5 {
313+ let d = word [l ];
314+ l += 1 ;
315+ if is_vowel (d ) {
316+ let count = cnt . entry (d ). or_insert (0 );
317+ * count -= 1 ;
318+ if * count == 0 {
319+ cnt . remove (& d );
320+ }
321+ } else {
322+ x -= 1 ;
323+ }
324+ }
325+ ans += l as i64 ;
326+ }
327+ ans
328+ }
329+
330+ let chars : Vec <char > = word . chars (). collect ();
331+ f (& chars , k ) - f (& chars , k + 1 )
332+ }
333+ }
334+ ```
335+
292336<!-- tabs:end -->
293337
294338<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn count_of_substrings ( word : String , k : i32 ) -> i64 {
3+ fn f ( word : & Vec < char > , k : i32 ) -> i64 {
4+ let mut ans = 0_i64 ;
5+ let mut l = 0 ;
6+ let mut x = 0 ;
7+ let mut cnt = std:: collections:: HashMap :: new ( ) ;
8+
9+ let is_vowel = |c : char | matches ! ( c, 'a' | 'e' | 'i' | 'o' | 'u' ) ;
10+
11+ for ( r, & c) in word. iter ( ) . enumerate ( ) {
12+ if is_vowel ( c) {
13+ * cnt. entry ( c) . or_insert ( 0 ) += 1 ;
14+ } else {
15+ x += 1 ;
16+ }
17+
18+ while x >= k && cnt. len ( ) == 5 {
19+ let d = word[ l] ;
20+ l += 1 ;
21+ if is_vowel ( d) {
22+ let count = cnt. entry ( d) . or_insert ( 0 ) ;
23+ * count -= 1 ;
24+ if * count == 0 {
25+ cnt. remove ( & d) ;
26+ }
27+ } else {
28+ x -= 1 ;
29+ }
30+ }
31+ ans += l as i64 ;
32+ }
33+ ans
34+ }
35+
36+ let chars: Vec < char > = word. chars ( ) . collect ( ) ;
37+ f ( & chars, k) - f ( & chars, k + 1 )
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments