Skip to content

Commit 32ec5f4

Browse files
committed
Create 806-number-of-lines-to-write-string.rs
1 parent 4552a24 commit 32ec5f4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub fn number_of_lines(widths: Vec<i32>, s: String) -> Vec<i32> {
2+
use std::collections::HashMap;
3+
let mut mmp = HashMap::new();
4+
let cs = vec!['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
5+
for (i, c) in cs.iter().enumerate() {
6+
mmp.insert(c, widths[i]);
7+
}
8+
let mut current = 0;
9+
let mut line = 1;
10+
for c in s.chars().collect::<Vec<char>>() {
11+
if current + mmp[&c] > 100 {
12+
line += 1;
13+
current = mmp[&c];
14+
} else {
15+
current += mmp[&c];
16+
}
17+
}
18+
vec![line, current]
19+
}
20+
21+
fn main() {
22+
let widths = vec![10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10];
23+
let s = "abcdefghijklmnopqrstuvwxyz".to_string();
24+
println!("{:?}", number_of_lines(widths, s));
25+
let widths = vec![4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10];
26+
let s = "bbbcccdddaaa".to_string();
27+
println!("{:?}", number_of_lines(widths, s));
28+
}

0 commit comments

Comments
 (0)