-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsh_sub.rs
60 lines (52 loc) · 2.12 KB
/
sh_sub.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
// Given string s, find the shortest substring that contains all chars appeared in s.
use std::collections::HashSet;
pub fn shortest_substring(s: &str) -> &str {
if s.is_empty() {
return s;
}
let mut unique_chars: HashSet<char> = HashSet::new();
for ch in s.chars() {
unique_chars.insert(ch);
}
let num_of_elements = unique_chars.len();
if num_of_elements == 1 {
return &s[0..1];
}
let mut solutions: Vec<&str> = Vec::new();
let mut temp_chars: HashSet<char> = HashSet::with_capacity(num_of_elements);
let mut start_index: usize = 0;
let mut index_of_shortest_substr: usize = 0;
loop {
temp_chars.clear();
for (i, ch) in s[start_index..].chars().enumerate() {
temp_chars.insert(ch);
if temp_chars.len() == num_of_elements {
let end_index = start_index + i;
// we call go_backwards with the slice shorter by 1 element because current "ch"
// definitely belongs to the substring
let offset_in_given_slice = go_backwards(&s[start_index..end_index], num_of_elements-1);
let new_start_index = start_index + offset_in_given_slice;
// end_index+1 to include last element
solutions.push(&s[new_start_index..end_index+1]);
if end_index - new_start_index < solutions[index_of_shortest_substr].len() {
index_of_shortest_substr = solutions.len() - 1;
}
start_index = new_start_index + 1;
break;
}
}
if temp_chars.len() != num_of_elements {break;}
}
solutions[index_of_shortest_substr]
}
// Return start index of the shortest substring in given slice.
fn go_backwards(s: &str, num_of_el: usize) -> usize {
let mut temp_chars: HashSet<char> = HashSet::with_capacity(num_of_el);
for (i, ch) in s.chars().rev().enumerate() {
temp_chars.insert(ch);
if temp_chars.len() == num_of_el {
return s.len()-i-1;
}
}
panic!("There is no substring, although it MUST be there!");
}