Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.
  • Note:
The vowels does not include the letter "y".
  • Examples:
Given s = "hello", return "holle".
Given s = "leetcode", return "leotcede".
  • Analysis:
Use two pointers, left and right to find vowels.

Left pointer is from 0 to right.

Right pointer is from end to left.

While finding vowels, swap them.
  • Code - Java:
class Solution {
    public String reverseVowels(String s) {
        int len = s.length();
        int left = 0;
        int right = len - 1;
        char[] arr = s.toCharArray();
        while (left <= right) {
            if (left <= right && !isVowel(arr[left])) {
                left++;
                continue;
            }
            if (right >= left && !isVowel(arr[right])) {
                right--;
                continue;
            }
            swap(arr, left, right);
            left++;
            right--;
        }
        return new String(arr);

    }
    private boolean isVowel(char ch) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch =='O' || ch == 'U') {
            return true;
        }
        return false;
    }
    private void swap(char[] arr, int left, int right) {
        char ch = arr[left];
        arr[left] = arr[right];
        arr[right] = ch;
    }
}
  • Code - C++:
class Solution {
public:
    string reverseVowels(string s) {
        int len = s.length();
        int left = 0;
        int right = len - 1;
        while (left <= right) {
            if (left <= right && !isVowel(s[left])) {
                left++;
                continue;
            }
            if (right >= left && !isVowel(s[right])) {
                right--;
                continue;
            }
            swap(s, left, right);
            left++;
            right--;
        }
        return s;

    }
    bool isVowel(char ch) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch =='O' || ch == 'U') {
            return true;
        }
        return false;
    }
    void swap(string &s, int left, int right) {
        char ch = s[left];
        s[left] = s[right];
        s[right] = ch;
    }
};
  • Code - Python:
class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        sLen, left, right = len(s), 0, len(s) - 1
        sList = list(s)
        while left <= right:
            if left <= right and not self.isVowel(s[left]):
                left += 1
                continue
            if right >= left and not self.isVowel(s[right]):
                right -= 1
                continue
            sList[left], sList[right] = sList[right], sList[left]
            left += 1
            right -= 1
        return ''.join(sList)
    def isVowel(self, ch):
        if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' or ch == 'A' or ch == 'E' or ch == 'I' or ch =='O' or ch == 'U':
            return True
        return False
  • Time Complexity: O(N), N is the length of string

  • Space Complexity: O(N), N is the length of string

results matching ""

    No results matching ""