File tree Expand file tree Collapse file tree 2 files changed +78
-2
lines changed Expand file tree Collapse file tree 2 files changed +78
-2
lines changed Original file line number Diff line number Diff line change 1
- mod p3208 ;
1
+ mod p3306 ;
2
2
3
3
pub fn main ( ) {
4
- p3208 :: run ( ) ;
4
+ p3306 :: run ( ) ;
5
5
}
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
3
+ pub fn run ( ) {
4
+ for i in [
5
+ ( "aeioqq" , 1 ) ,
6
+ ( "aeiou" , 0 ) ,
7
+ ( "ieaouqqieaouqq" , 1 )
8
+ ] {
9
+ println ! ( "{}" , count_of_substrings( i. 0 . to_string( ) , i. 1 ) ) ;
10
+ }
11
+ }
12
+
13
+ pub fn count_of_substrings ( word : String , k : i32 ) -> i64 {
14
+ let chars = word. chars ( ) . collect :: < Vec < char > > ( ) ;
15
+
16
+ at_least ( & chars, k) - at_least ( & chars, k + 1 )
17
+ }
18
+
19
+ pub fn at_least ( chars : & Vec < char > , k : i32 ) -> i64 {
20
+ let is_vowel = |x : char | ( x == 'a' ) || ( x == 'e' ) || ( x == 'i' ) || ( x == 'o' ) || ( x == 'u' ) ;
21
+
22
+ let l = chars. len ( ) ;
23
+
24
+ let mut map: HashMap < char , i64 > = HashMap :: new ( ) ;
25
+ let mut consonant_count = 0 ;
26
+
27
+ let mut vowel_reached = false ;
28
+
29
+ let mut result = 0 ;
30
+
31
+ let mut left = 0 ;
32
+ let mut right = 0 ;
33
+ while ( right < l) || ( left < l) {
34
+ if ( consonant_count >= k) && vowel_reached {
35
+ // increment left
36
+ result += ( l - right + 1 ) as i64 ;
37
+
38
+ let next = chars[ left] ;
39
+ if is_vowel ( next) {
40
+ let m = map. get_mut ( & next) . unwrap ( ) ;
41
+ * m -= 1 ;
42
+ if * m <= 0 {
43
+ map. remove ( & next) ;
44
+ vowel_reached = map. len ( ) == 5 ;
45
+ }
46
+ } else {
47
+ consonant_count -= 1 ;
48
+ }
49
+ left += 1 ;
50
+ } else {
51
+ // consume forwards
52
+ if right >= l {
53
+ break ;
54
+ }
55
+
56
+ let next = chars[ right] ;
57
+ if is_vowel ( next) {
58
+ match map. get_mut ( & next) {
59
+ Some ( m) => {
60
+ * m += 1 ;
61
+ } ,
62
+ None => {
63
+ map. insert ( next, 1 ) ;
64
+ vowel_reached = map. len ( ) == 5 ;
65
+ }
66
+ }
67
+ } else {
68
+ consonant_count += 1 ;
69
+ }
70
+
71
+ right += 1 ;
72
+ }
73
+ }
74
+
75
+ result
76
+ }
You can’t perform that action at this time.
0 commit comments