Skip to content

Commit

Permalink
Merge 1bdcaef into e5b10ea
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Dec 15, 2018
2 parents e5b10ea + 1bdcaef commit 241acc0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ problems from
* [Day 109](https://github.com/vaskoz/dailycodingproblem-go/issues/229)
* [Day 110](https://github.com/vaskoz/dailycodingproblem-go/issues/231)
* [Day 111](https://github.com/vaskoz/dailycodingproblem-go/issues/232)
* [Day 113](https://github.com/vaskoz/dailycodingproblem-go/issues/236)
* [Day 114](https://github.com/vaskoz/dailycodingproblem-go/issues/237)
45 changes: 45 additions & 0 deletions day113/problem.go
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]
}
}
44 changes: 44 additions & 0 deletions day113/problem_test.go
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)
}
}
}

0 comments on commit 241acc0

Please sign in to comment.