-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0038_count_and_say.rs
More file actions
43 lines (34 loc) · 927 Bytes
/
Copy paths0038_count_and_say.rs
File metadata and controls
43 lines (34 loc) · 927 Bytes
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
#![allow(unused)]
pub struct Solution {}
impl Solution {
// O(2^n) O(2^n)
pub fn count_and_say(n: i32) -> String {
if n < 2 {
return "1".to_string();
}
let prev_res = Self::count_and_say(n - 1).chars().collect::<Vec<char>>();
let mut ret = vec![];
let mut prev_chr = &prev_res[0];
let mut count = 1;
for ch in &prev_res[1..] {
if ch == prev_chr {
count += 1;
} else {
ret.push(format!("{}{}", count, prev_chr));
count = 1;
}
prev_chr = ch;
}
ret.push(format!("{}{}", count, prev_chr));
ret.join("")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_38() {
assert_eq!(Solution::count_and_say(1,), "1".to_string());
assert_eq!(Solution::count_and_say(4,), "1211".to_string());
}
}