-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
64 lines (57 loc) · 2.1 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
fn main() {
let mut x = 0;
for i in x..11 {
x += 2;
println!("x = {:#?}", x);
}
// assert_eq!(Solution::remove_duplicate_letters(String::from("bcabc")), String::from("abc"));
}
struct Solution {}
impl Solution {
pub fn remove_duplicate_letters(s: String) -> String {
let s: Vec<char> = s.chars().collect();
use std::collections::HashMap;
let mut map: HashMap<char, usize> = HashMap::new();
let mut res: Vec<char> = vec![];
for i in 0..s.len() {
map.entry(s[i]).and_modify(|x| *x = i ).or_insert(i);
}
let mut prev = 0;
while !map.is_empty() {
// find the smallest idx
let (mut c, idx) = map.iter().fold((' ', s.len()), |sum, (&k, &v)| if v < sum.1 { (k, v) } else { sum });
// check whether smaller exist before
let mut changed = false;
for i in prev..idx {
if s[i] <= c && map.get(&s[i]).is_some() {
if s[i] < c {
c = s[i]; prev = i + 1; changed = true
} else if s[i] == c {
if !changed { prev = i + 1; changed = true }
}
}
}
if c == s[idx] && !changed { prev = idx + 1 }
map.remove(&c);
res.push(c);
}
res.iter().collect()
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::remove_duplicate_letters(String::from("bcabc")), String::from("abc"));
assert_eq!(Solution::remove_duplicate_letters(String::from("cbacdcbc")), String::from("acdb"));
assert_eq!(Solution::remove_duplicate_letters(String::from("aaabdddba")), String::from("abd"));
assert_eq!(Solution::remove_duplicate_letters(String::from("abacb")), String::from("abc"));
}
#[test]
fn fail() {
assert_eq!(Solution::remove_duplicate_letters(
String::from("eywdgenmcnzhztolafcfnirfpuxmfcenlppegrcalgxjlajxmphwidqqtrqnmmbssotoywfrtylm")),
String::from("chzafipuegjlxdqnbsotwrym"));
}
}