Skip to content

Commit e2c54bf

Browse files
authored
557 solved. (#63)
1 parent ba6f328 commit e2c54bf

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ continually updating 😃.
5454
* [344. Reverse String](./src/0344_reverse_string/reverse_string.go)   *`string;`*  *`double index`*
5555
* [345. Reverse Vowels of a String](./src/0345_reverse_vowels_of_a_string/reverse_vowels.go)   *`string;`*  *`double index`*
5656
* [438. Find All Anagrams in a String](./src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)   *`sliding window`*
57+
* [557. Reverse Words in a String III](src/0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)
5758

5859
### Linked List
5960
* [2. Add Two Numbers](./src/0002_add_two_numbers/add_two_numbers.go)   *`recursion;`*  *`math`*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
557. Reverse Words in a String III
3+
https://leetcode.com/problems/reverse-words-in-a-string-iii/
4+
5+
Given a string, you need to reverse the order of characters in each word within a sentence
6+
while still preserving whitespace and initial word order.
7+
8+
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
9+
*/
10+
// time: 2019-01-08
11+
12+
package rwias3
13+
14+
import "strings"
15+
16+
// split and reverse
17+
// time complexity: O(len(s))
18+
// space complexity: O(n)
19+
func reverseWords(s string) string {
20+
words := strings.Split(s, " ")
21+
22+
var temp []string
23+
for _, word := range words {
24+
temp = append(temp, reverseString(word))
25+
}
26+
return strings.Join(temp, " ")
27+
}
28+
29+
func reverseString(s string) string {
30+
runes := []rune(s)
31+
32+
for start, end := 0, len(runes)-1; start < end; start, end = start+1, end-1 {
33+
runes[start], runes[end] = runes[end], runes[start]
34+
}
35+
return string(runes)
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package rwias3
2+
3+
import "testing"
4+
5+
func TestReverseWords(t *testing.T) {
6+
s := "Let's take LeetCode contest"
7+
expected := "s'teL ekat edoCteeL tsetnoc"
8+
9+
if res := reverseWords(s); res != expected {
10+
t.Errorf("expected %s, got %s", expected, res)
11+
}
12+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
|0447|[Number of Boomerangs](./0447_number_of_boomerangs/number_of_boomerangs.go)|Easy||
9595
|0454|[4Sum II](./0454_4sum2/4sum2.go)|Medium||
9696
|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|
97+
|0557|[557. Reverse Words in a String III](0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)|Easy|*`string`*|
9798
|0674|[674. Longest Continuous Increasing Subsequence](0674_longest_continuous_increasing_subsequence/lcis.go)|Easy||
9899
|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|
99100
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|

0 commit comments

Comments
 (0)