-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpositions-of-large-groups.rs
52 lines (45 loc) · 1.23 KB
/
positions-of-large-groups.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
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!(
vec![vec![3, 6]],
Solution::large_group_positions("abbxxxxzzy".to_string())
);
assert_eq!(
Vec::<Vec<i32>>::new(),
Solution::large_group_positions("abc".to_string())
);
assert_eq!(
vec![vec![3, 5], vec![6, 9], vec![12, 14]],
Solution::large_group_positions("abcdddeeeeaabbbcd".to_string())
);
assert_eq!(
vec![vec![0, 2]],
Solution::large_group_positions("aaa".to_string())
);
}
struct Solution;
impl Solution {
pub fn large_group_positions(s: String) -> Vec<Vec<i32>> {
let mut v = Vec::<Vec<i32>>::new();
let s = s.as_bytes();
let mut start = 0usize;
let mut end = 0usize;
for (index, &value) in s.iter().enumerate() {
if index == 0 {
continue;
}
if value == s[index - 1] {
end = index;
if end != s.len() - 1 {
continue;
}
}
if end - start >= 2 {
v.push(vec![start as i32, end as i32]);
}
start = index;
end = index;
}
v
}
}