Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c289a7b

Browse files
authored
Create 0557-reverse-words-in-a-string-iii.kt
1 parent 7f39896 commit c289a7b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
fun reverseWords(s: String): String {
3+
val words = s.split(" ")
4+
val res = LinkedList<String>()
5+
6+
for (word in words) {
7+
if (res.isNotEmpty())
8+
res.add(" ")
9+
val sb = StringBuilder()
10+
for (i in word.lastIndex downTo 0) {
11+
sb.append(word[i])
12+
}
13+
res.add(sb.toString())
14+
}
15+
16+
return res.joinToString("")
17+
}
18+
}

0 commit comments

Comments
 (0)