Skip to content

Commit b5c2893

Browse files
committed
solve: 151. Reverse Words in a String in rust & golang
Signed-off-by: rajput-hemant <[email protected]>
1 parent 68b619e commit b5c2893

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func reverseWords(s string) string {
8+
words := strings.Fields(s) // split the string by whitespace
9+
10+
// reverse the order of the words
11+
for i, j := 0, len(words)-1; i < j; i, j = i+1, j-1 {
12+
words[i], words[j] = words[j], words[i]
13+
}
14+
15+
return strings.Join(words, " ")
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn reverse_words(mut s: String) -> String {
3+
s.split_whitespace().rev().collect::<Vec<_>>().join(" ")
4+
}
5+
}

0 commit comments

Comments
 (0)