-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package day113 | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// ReverseWords reverses the word position in the string. | ||
// Runs in O(N) time and requires O(N) additional space. | ||
func ReverseWords(str string) string { | ||
words := strings.Split(str, " ") | ||
for i := 0; i < len(words)/2; i++ { | ||
words[i], words[len(words)-1-i] = words[len(words)-1-i], words[i] | ||
} | ||
return strings.Join(words, " ") | ||
} | ||
|
||
// ReverseWordsInPlace reverses the word position in the string. | ||
// Runs in O(N) time and requires O(1) additional space. | ||
func ReverseWordsInPlace(str string) string { | ||
r := []rune(str) | ||
for i := 0; i < len(r)/2; i++ { | ||
r[i], r[len(r)-1-i] = r[len(r)-1-i], r[i] | ||
} | ||
var start, end int | ||
for end <= len(r) { | ||
if end == len(r) { | ||
reverseInPlace(r, start, end) | ||
} else if r[end] == ' ' { | ||
reverseInPlace(r, start, end) | ||
start = end | ||
for r[start] == ' ' { | ||
start++ | ||
} | ||
end = start - 1 | ||
} | ||
end++ | ||
} | ||
return string(r) | ||
} | ||
|
||
func reverseInPlace(r []rune, start, end int) { | ||
for i := start; i < (start+end)/2; i++ { | ||
r[i], r[end-1-(i-start)] = r[end-1-(i-start)], r[i] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package day113 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
input, expected string | ||
}{ | ||
{"hello world here", "here world hello"}, | ||
{"greetings from colorado", "colorado from greetings"}, | ||
} | ||
|
||
func TestReverseWords(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := ReverseWords(tc.input); result != tc.expected { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkReverseWords(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
ReverseWords(tc.input) | ||
} | ||
} | ||
} | ||
|
||
func TestReverseWordsInPlace(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := ReverseWordsInPlace(tc.input); result != tc.expected { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkReverseWordsInPlace(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
ReverseWordsInPlace(tc.input) | ||
} | ||
} | ||
} |