Reversing Vowels in a String Using Two Pointers in Golang
Tags: go, tutorial, leetcode
Published: 2023-10-08 09:02:24

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:

1. Initialize a string variable `vowels` containing all lowercase and uppercase vowel characters: "aeiouAEIOU."

2. 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.

3. Initialize two pointers, `left` and `right`, to 0 and `len(s)-1`, respectively, where `s` is the input string.

4. Use a while loop to iterate through the string while the `left` pointer is less than the `right` pointer.

5. Within the loop:
   - Increment the `left` pointer until a vowel character is found or until `left` is no longer less than `right`.
   - Decrement the `right` pointer until a vowel character is found or until `left` is no longer less than `right`.

6. If the `left` pointer is still less than the `right` pointer, swap the characters at these positions. Then, increment the `left` pointer and decrement the `right` pointer.

7. After the loop, return the string representation of the modified `strRunes`.

## The Code

Here's the Go code implementing the described approach:

```go
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:

```go
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.