Skip to content

Commit 52ffb24

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 3.40 MB (Top 25.0%)
1 parent e5eef72 commit 52ffb24

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 3.40 MB (Top 25.0%)
2+
3+
impl Solution {
4+
pub fn appeal_sum(s: String) -> i64 {
5+
let n = s.len();
6+
let mut f = vec![0; n];
7+
let mut last_pos = [-1; 26];
8+
9+
// println!("last_pos {:?}", last_pos);
10+
11+
for (i, c) in s.chars().enumerate() {
12+
let char_id = c as usize - 'a' as usize;
13+
let i2 = i as i64;
14+
// println!("char id {}", char_id);
15+
if i == 0 {
16+
f[i] = 1;
17+
last_pos[char_id] = i2;
18+
continue;
19+
}
20+
f[i] += f[i-1] + i2 - last_pos[char_id];
21+
last_pos[char_id] = i2;
22+
}
23+
24+
let ans = f.iter().sum();
25+
26+
ans
27+
}
28+
}

0 commit comments

Comments
 (0)