Skip to content

Commit 081845c

Browse files
authored
Added solution for 1859
1 parent 91caeb7 commit 081845c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: 01859. Sorting the Sentence.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
// Split sentence into a vector. Then create a blank vector of same size. Then loop through the first vector and add words in the second vector as per the number within the string. Then join the second vector and return it
3+
pub fn sort_sentence(s: String) -> String {
4+
let words_split: Vec<_> = s.split(" ").collect();
5+
6+
let mut result_words_split = vec![""; words_split.len()];
7+
8+
for word in words_split {
9+
let position_index = (word.chars().nth(word.chars().count() - 1).unwrap()) as usize - 49;
10+
result_words_split[position_index] = &word[0..(word.chars().count() - 1) as usize];
11+
}
12+
13+
return result_words_split.join(" ");
14+
}
15+
}

0 commit comments

Comments
 (0)