When working with strings in Go (Golang), you may come across scenarios where you need to manipulate the characters within the string. One common task is reversing the positions of vowels while keeping consonants and other characters in their original places. In this blog post, we will explore how to achieve this using a simple and efficient approach with the help of two pointers.
The Problem
Given an input string, we want to reverse the positions of vowels (i.e., 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') while preserving the positions of consonants and other characters. For example, if the input string is "hello," the output should be "holle."
The Approach
To solve this problem, we will use two pointers, one starting from the beginning of the string (left) and the other starting from the end of the string (right). We will iterate through the string and swap vowels found at these two pointers. The process continues until the left pointer is less than the right pointer.
Here's the step-by-step approach:
Initialize a string variable
vowels
containing all lowercase and uppercase vowel characters: "aeiouAEIOU."Convert the input string into a mutable rune slice called
strRunes
. In Go, strings are immutable, so we need to work with runes to make modifications.Initialize two pointers,
left
andright
, to 0 andlen(s)-1
, respectively, wheres
is the input string.Use a while loop to iterate through the string while the
left
pointer is less than theright
pointer.-
Within the loop:
- Increment the
left
pointer until a vowel character is found or untilleft
is no longer less thanright
. - Decrement the
right
pointer until a vowel character is found or untilleft
is no longer less thanright
.
- Increment the
If the
left
pointer is still less than theright
pointer, swap the characters at these positions. Then, increment theleft
pointer and decrement theright
pointer.After the loop, return the string representation of the modified
strRunes
.
The Code
Here's the Go code implementing the described approach:
func reverseVowels(s string) string {
vowels := "aeiouAEIOU"
strRunes := []rune(s)
left, right := 0, len(s)-1
for left < right {
for left < right && !strings.ContainsRune(vowels, strRunes[left]) {
left++
}
for left < right && !strings.ContainsRune(vowels, strRunes[right]) {
right--
}
if left < right {
strRunes[left], strRunes[right] = strRunes[right], strRunes[left]
left++
right--
}
}
return string(strRunes)
}
Example Usage
Let's see how to use the reverseVowels
function with some examples:
fmt.Println(reverseVowels("hello")) // Output: "holle"
fmt.Println(reverseVowels("leetcode")) // Output: "leotcede"
fmt.Println(reverseVowels("GoLang")) // Output: "GoLeNg"
Conclusion
Reversing vowels in a string is a common string manipulation task that can be efficiently solved using two pointers in Go. By following the approach described in this blog post, you can easily reverse the positions of vowels while maintaining the order of consonants and other characters in the string. This technique can be handy in various text processing scenarios where you need to modify or analyze text data.
Top comments (0)