File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 1
- mod p3306 ;
1
+ mod p1358 ;
2
2
3
3
pub fn main() {
4
- p3306 ::run();
4
+ p1358 ::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
+ "abcabc",
6
+ "aaacb",
7
+ "abc"
8
+ ] {
9
+ println!("{}", number_of_substrings(i.to_string()));
10
+ }
11
+ }
12
+
13
+ pub fn number_of_substrings(s: String) -> i32 {
14
+ let chars = s.chars().collect::<Vec<char>>();
15
+ let a = 'a' as usize;
16
+
17
+ let mut map: Vec<i32> = vec![0; 3];
18
+
19
+ let width = chars.len();
20
+ let mut result = 0;
21
+ let mut i = 0;
22
+ let mut j = 0;
23
+
24
+ while j < width {
25
+ map[(chars[j] as usize) - a] += 1;
26
+
27
+ while (map[0] >= 1) && (map[1] >= 1) && (map[2] >= 1) {
28
+ result += width - j;
29
+
30
+ map[(chars[i] as usize) - a] -= 1;
31
+ i += 1;
32
+ }
33
+
34
+ j += 1;
35
+ }
36
+
37
+ result as i32
38
+ }
You can’t perform that action at this time.
0 commit comments