Skip to content

Commit 1980d89

Browse files
committed
[String] Add a solution to Reverse Words in a String I, II, and III
1 parent caafd3c commit 1980d89

5 files changed

+84
-4
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 266 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 269 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -96,6 +96,9 @@
9696
[Reverse String](https://leetcode.com/problems/reverse-string/)| [Swift](./String/ReverseString.swift)| Easy| O(n)| O(n)|
9797
[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)| [Swift](./String/ReverseStringII.swift)| Easy| O(n)| O(n)|
9898
[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)| [Swift](./String/ReverseVowelsOfAString.swift)| Easy| O(n)| O(n)|
99+
[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)| [Swift](./String/ReverseWordsString.swift)| Medium| O(n)| O(1)|
100+
[Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/)| [Swift](./String/ReverseWordsStringII.swift)| Medium| O(n)| O(1)|
101+
[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| [Swift](./String/ReverseWordsStringIII.swift)| Easy| O(n)| O(1)|
99102
[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)| [Swift](./String/AddStrings.swift)| Easy| O(n)| O(n)|
100103
[String Compression](https://leetcode.com/problems/string-compression/)| [Swift](./String/StringCompression.swift)| Easy| O(n)| O(1)|
101104
[Add Strings](https://leetcode.com/problems/add-strings/)| [Swift](./String/LengthLastWord.swift)| Easy| O(n)| O(1)|
@@ -649,7 +652,7 @@
649652
| [Swift](./Array/RotateArray.swift) | 189 | [Rotate Array](https://oj.leetcode.com/problems/rotate-array/) | Easy |
650653
| [Swift](./DP/BestTimeBuySellStockIV.swift) | 188 | [Best Time to Buy and Sell Stock IV](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/) | Hard |
651654
| | 187 | [Repeated DNA Sequences](https://oj.leetcode.com/problems/repeated-dna-sequences/) | Medium |
652-
| | 186 | [Reverse Words in a String II](https://oj.leetcode.com/problems/reverse-words-in-a-string-ii/) ♥ | Medium |
655+
| [Swift](./String/ReverseWordsStringII.swift) | 186 | [Reverse Words in a String II](https://oj.leetcode.com/problems/reverse-words-in-a-string-ii/) ♥ | Medium |
653656
| | 179 | [Largest Number](https://oj.leetcode.com/problems/largest-number/) | Medium |
654657
| | 174 | [Dungeon Game](https://oj.leetcode.com/problems/dungeon-game/) | Hard |
655658
| | 173 | [Binary Search Tree Iterator](https://oj.leetcode.com/problems/binary-search-tree-iterator/) | Medium |
@@ -674,7 +677,7 @@
674677
| [Swift](./Search/FindMinimumRotatedSortedArrayII.swift) | 154 | [Find Minimum in Rotated Sorted Array II](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) | Hard |
675678
| [Swift](./Search/FindMinimumRotatedSortedArray.swift) | 153 | [Find Minimum in Rotated Sorted Array](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | Medium |
676679
| [Swift](./DP/MaximumProductSubarray.swift) | 152 | [Maximum Product Subarray](https://oj.leetcode.com/problems/maximum-product-subarray/) | Medium |
677-
| | 151 | [Reverse Words in a String](https://oj.leetcode.com/problems/reverse-words-in-a-string/) | Medium |
680+
| [Swift](./String/ReverseWordsString.swift) | 151 | [Reverse Words in a String](https://oj.leetcode.com/problems/reverse-words-in-a-string/) | Medium |
678681
| [Swift](./Stack/EvaluateReversePolishNotation.swift) | 150 | [Evaluate Reverse Polish Notation](https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/) | Medium |
679682
| | 149 | [Max Points on a Line](https://oj.leetcode.com/problems/max-points-on-a-line/) | Hard |
680683
| | 148 | [Sort List](https://oj.leetcode.com/problems/sort-list/) | Medium |

String/ReverseString.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
class ReverseString {
1010
func reverseString(s: String) -> String {
11-
return String(s.characters.reverse())
11+
return String(s.reversed())
1212
}
1313
}

String/ReverseWordsString.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/reverse-words-in-a-string/
3+
* Primary idea: Trim and split the original string, add word from end to start
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class ReverseWordsString {
10+
func reverseWords(_ s: String) -> String {
11+
return String(s.split(separator: " ").reversed().reduce("") { total, word in total + word + " "}.dropLast())
12+
}
13+
}

String/ReverseWordsStringII.swift

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/reverse-words-in-a-string-ii/
3+
* Primary idea: Reverse the whole string, then reverse every word
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class ReverseWordsStringII {
10+
func reverseWords(_ str: inout [Character]) {
11+
var last = 0
12+
13+
reverse(&str, 0, str.count - 1)
14+
15+
for i in 0..<str.count {
16+
if i + 1 == str.count || (str[i] != " " && str[i + 1] == " ") {
17+
reverse(&str, last, i)
18+
last = i + 2
19+
}
20+
}
21+
}
22+
23+
fileprivate func reverse<T>(_ array: inout [T], _ startIdx: Int, _ endIdx: Int) {
24+
var (left, right) = (startIdx, endIdx)
25+
26+
while left < right {
27+
(array[left], array[right]) = (array[right], array[left])
28+
left += 1
29+
right -= 1
30+
}
31+
}
32+
}

String/ReverseWordsStringIII.swift

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/reverse-words-in-a-string-iii/
3+
* Primary idea: Check the empty space to get the previous word, then reverse it
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class ReverseWordsStringIII {
10+
func reverseWords(_ s: String) -> String {
11+
var last = 0, str = Array(s)
12+
13+
for i in 0..<str.count {
14+
if i + 1 == str.count || (str[i] != " " && str[i + 1] == " ") {
15+
reverse(&str, last, i)
16+
last = i + 2
17+
}
18+
}
19+
20+
return String(str)
21+
}
22+
23+
fileprivate func reverse<T>(_ array: inout [T], _ startIdx: Int, _ endIdx: Int) {
24+
var (left, right) = (startIdx, endIdx)
25+
26+
while left < right {
27+
(array[left], array[right]) = (array[right], array[left])
28+
left += 1
29+
right -= 1
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)