Skip to content

Commit cfe1339

Browse files
committed
p1358
1 parent 1c51bef commit cfe1339

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
mod p3306;
1+
mod p1358;
22

33
pub fn main() {
4-
p3306::run();
4+
p1358::run();
55
}

src/p1358.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

0 commit comments

Comments
 (0)