-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01333. Print Words Vertically.rs
45 lines (40 loc) · 1.67 KB
/
01333. Print Words Vertically.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
use std::cmp;
// Split the sentence into words. Calculate size of output vector's each element since we'll have to add spaces where words are smaller. Loop through the words and add them in the output vector
impl Solution {
pub fn print_vertically(s: String) -> Vec<String> {
let mut iter = s.split_whitespace();
let mut current_iter = iter.next();
let mut words = Vec::new();
let mut output = Vec::new();
let mut output_sizes = Vec::new();
// Split the sentence into words
while(current_iter != None) {
words.push(current_iter.unwrap());
current_iter = iter.next();
}
// Calculate sizes of output vector's each element since we'll have to add spaces where words are smaller
for s in words.iter().rev() {
if output_sizes.len() == 0 {
output_sizes.push(s.len());
} else {
output_sizes.insert(0, cmp::max(output_sizes[0], s.len()));
}
}
// Loop through the words and add them in the output vector
for (idx, s) in words.iter().enumerate() {
for inner_idx in 0..output_sizes[idx] {
if (output.len() <= inner_idx) {
output.push(String::new());
}
let mut current_char;
if (s.len() <= inner_idx) {
current_char = ' ';
} else {
current_char = s.chars().nth(inner_idx).unwrap();
}
output[inner_idx] = format!("{}{}", output[inner_idx], current_char);
}
}
return output;
}
}