-
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
68 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,38 @@ | ||
package day114 | ||
|
||
import "strings" | ||
|
||
// ReverseWordsMaintainDelimters reverses the words in the string | ||
// while maintaining the relative order of the delimiters | ||
// Works only with two delimiters "/" or ":" | ||
// Runs in O(N) time and requires O(N) extra space to construct the result. | ||
func ReverseWordsMaintainDelimters(str string) string { | ||
var start, end int | ||
var words []string | ||
var delimiters []string | ||
for end <= len(str) { | ||
if end == len(str) { | ||
if str[end-1] != ':' && str[end-1] != '/' { | ||
words = append(words, str[start:end]) | ||
} | ||
break | ||
} | ||
if str[end] == '/' || str[end] == ':' { | ||
delimiters = append(delimiters, string(str[end])) | ||
words = append(words, str[start:end]) | ||
start = end + 1 | ||
} | ||
end++ | ||
} | ||
for i := 0; i < len(words)/2; i++ { | ||
words[i], words[len(words)-1-i] = words[len(words)-1-i], words[i] | ||
} | ||
var result []string | ||
if len(delimiters) != len(words) { | ||
delimiters = append([]string{""}, delimiters...) | ||
} | ||
for i := range words { | ||
result = append(result, delimiters[i], words[i]) | ||
} | ||
return strings.Join(result, "") | ||
} |
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,29 @@ | ||
package day114 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
input, expected string | ||
}{ | ||
{"hello/world:here", "here/world:hello"}, | ||
{"hello/world:here/", "/here:world/hello"}, | ||
{"hello//world:here", "here/world/:hello"}, | ||
{"/hello//world:here/", "/here/world/:hello/"}, | ||
} | ||
|
||
func TestReverseWordsMaintainDelimters(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := ReverseWordsMaintainDelimters(tc.input); result != tc.expected { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkReverseWordsMaintainDelimters(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
ReverseWordsMaintainDelimters(tc.input) | ||
} | ||
} | ||
} |