Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add: Reverse Vowels of a String
  • Loading branch information
Openset committed Jan 8, 2019
commit 091baa753d3dd73688f095babfb3ccfb779ef94e
24 changes: 24 additions & 0 deletions problems/reverse-vowels-of-a-string/reverse_vowels_of_a_string.go
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
package reverse_vowels_of_a_string

func reverseVowels(s string) string {
i, j, ss := 0, len(s)-1, []byte(s)
for i < j {
if !isVowel(ss[i]) {
i++
} else if !isVowel(ss[j]) {
j--
} else {
ss[i], ss[j] = ss[j], ss[i]
i++
j--
}
}
return string(ss)
}

func isVowel(r byte) bool {
switch r {
case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':
return true
}
return false
}
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
package reverse_vowels_of_a_string

import "testing"

type caseType struct {
input string
expected string
}

func TestReverseVowels(t *testing.T) {
tests := [...]caseType{
{
input: "hello",
expected: "holle",
},
{
input: "leetcode",
expected: "leotcede",
},
}
for _, tc := range tests {
output := reverseVowels(tc.input)
if output != tc.expected {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}