Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 091baa7

Browse files
author
Openset
committed
Add: Reverse Vowels of a String
1 parent fc6d8b5 commit 091baa7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
package reverse_vowels_of_a_string
2+
3+
func reverseVowels(s string) string {
4+
i, j, ss := 0, len(s)-1, []byte(s)
5+
for i < j {
6+
if !isVowel(ss[i]) {
7+
i++
8+
} else if !isVowel(ss[j]) {
9+
j--
10+
} else {
11+
ss[i], ss[j] = ss[j], ss[i]
12+
i++
13+
j--
14+
}
15+
}
16+
return string(ss)
17+
}
18+
19+
func isVowel(r byte) bool {
20+
switch r {
21+
case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':
22+
return true
23+
}
24+
return false
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package reverse_vowels_of_a_string
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected string
8+
}
9+
10+
func TestReverseVowels(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "hello",
14+
expected: "holle",
15+
},
16+
{
17+
input: "leetcode",
18+
expected: "leotcede",
19+
},
20+
}
21+
for _, tc := range tests {
22+
output := reverseVowels(tc.input)
23+
if output != tc.expected {
24+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /