Skip to content

Commit 62aefd3

Browse files
committed
Reverse words in a string III.
1 parent 3797c6c commit 62aefd3

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
3+
4+
 
5+
6+
Example 1:
7+
Input: s = "Let's take LeetCode contest"
8+
Output: "s'teL ekat edoCteeL tsetnoc"
9+
10+
Example 2:
11+
Input: s = "God Ding"
12+
Output: "doG gniD"
13+
 
14+
15+
Constraints:
16+
- 1 <= s.length <= 5 * 104
17+
- s contains printable ASCII characters.
18+
- s does not contain any leading or trailing spaces.
19+
- There is at least one word in s.
20+
- All the words in s are separated by a single space.
21+
*/
22+
class Solution {
23+
func reverseWords(_ s: String) -> String {
24+
let wordsArr = s.split(separator: " ")
25+
var result = ""
26+
for word in wordsArr {
27+
result.append("\(String(word.reversed())) ")
28+
}
29+
result.removeLast()
30+
return result
31+
}
32+
}
33+
34+
let s = Solution()
35+
let r = s.reverseWords("God Ding")
36+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/557.Reverse Words in a String III.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
105. [Longest Uncommon Subsequence I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/521.Longest%20Uncommon%20Subsequence%20I.playground/Contents.swift)
110110
106. [Reverse String II](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/541.Reverse%20String%20II.playground/Contents.swift)
111111
107. [Student Attendance Record I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/551.Student%20Attendance%20Record%20I.playground/Contents.swift)
112+
108. [Reverse Words in a String III](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/557.Reverse%20Words%20in%20a%20String%20III.playground/Contents.swift)
112113

113114
#### Medium
114115

0 commit comments

Comments
 (0)