-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0316_remove_duplicate_letters.rs
49 lines (46 loc) · 1.28 KB
/
s0316_remove_duplicate_letters.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![allow(unused)]
pub struct Solution {}
impl Solution {
pub fn remove_duplicate_letters(s: String) -> String {
let mut counts: [usize; 26] = [0; 26];
for &b in s.as_bytes() {
counts[(b - b'a') as usize] += 1;
}
let mut stack: Vec<char> = Vec::with_capacity(26);
let mut exists: [bool; 26] = [false; 26];
for &b in s.as_bytes() {
let i = (b - b'a') as usize;
counts[i] -= 1;
if exists[i] {
continue;
}
while let Some(&last) = stack.last() {
let j = (last as u8 - b'a') as usize;
if b < last as u8 && counts[j] > 0 {
exists[j] = false;
stack.pop();
} else {
break;
}
}
stack.push(b as char);
exists[i] = true;
}
stack.into_iter().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_293() {
assert_eq!(
Solution::remove_duplicate_letters("bcabc".to_string(),),
"abc".to_string()
);
assert_eq!(
Solution::remove_duplicate_letters("cbacdcbc".to_string(),),
"acdb".to_string()
);
}
}