Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 433ab83

Browse files
committed
# 557. Reverse Words in a String III
1 parent 2caf2df commit 433ab83

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

557. Reverse Words in a String III.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 557. Reverse Words in a String III
2+
3+
### 2017-04-09
4+
5+
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
6+
7+
**Example 1:**
8+
9+
```
10+
Input: "Let's take LeetCode contest"
11+
Output: "s'teL ekat edoCteeL tsetnoc"
12+
13+
```
14+
15+
**Note:** In the string, each word is separated by single space and there will not be any extra space in the string.
16+
17+
18+
19+
# Solution
20+
21+
```swift
22+
class Solution {
23+
func reverseWords(_ s: String) -> String {
24+
return s.components(separatedBy: " ")
25+
.flatMap({ $0.characters.reversed().reduce("", { $0 + $1.description})})
26+
.reduce("", { $0.isEmpty ? $1 : "\($0) \($1)" })
27+
28+
}
29+
}
30+
```
31+

0 commit comments

Comments
 (0)