Welcome to Subscribe On Youtube

Question

Formatted question description: https://leetcode.ca/all/345.html

 345	Reverse Vowels of a String

 Write a function that takes a string as input and reverse only the vowels of a string.

 Example 1:

 Input: "hello"
 Output: "holle"

 Example 2:

 Input: "leetcode"
 Output: "leotcede"

 Note:
 The vowels does not include the letter "y".

 @tag-string

Algorithm

It should be noted that capital letters are counted, so there are ten letters in total.

We write a isVowel function to determine whether the current character is a vowel,

  • If both sides are vowels, then we exchange,
  • If the one on the left is not, move one place to the right,
  • If the one on the right is not, move one place to the left

Code

Java

  • 
    public class Reverse_Vowels_of_a_String {
    
        class Solution {
            public String reverseVowels(String s) {
                char[] a = s.toCharArray();
    
                int l = 0; // left
                int r = a.length - 1; // right
    
                while (l < r) {
                    if (isVowel(a[l]) && isVowel(a[r])) {
                        swap(a, l++, r--);
                    } else if (isVowel(a[l])) {
                        r--;
                    } else {
                        l++;
                    }
                }
    
                return new String(a);
            }
    
            private boolean isVowel(char b) { // The letters A, E, I, O, and U are called vowels
                char c = Character.toLowerCase(b);
                return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
            }
    
            private void swap(char[] cs, int start, int end) {
                char temp = cs[start];
                cs[start] = cs[end];
                cs[end] = temp;
            }
        }
    }
    
  • // OJ: https://leetcode.com/problems/reverse-vowels-of-a-string/
    // Time: O(N)
    // Space: O(1)
    class Solution {
    private:
        bool isVowel(char c) {
            c = tolower(c);
            return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
        }
    public:
        string reverseVowels(string s) {
            int i = 0, j = s.size() - 1;
            while (i < j) {
                while (i < j && !isVowel(s[i])) ++i;
                while (i < j && !isVowel(s[j])) --j;
                if (i < j) swap(s[i++], s[j--]);
            }
            return s;
        }
    };
    
  • class Solution:
        def reverseVowels(self, s: str) -> str:
            vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
            i, j = 0, len(s) - 1
            chars = list(s)
            while i < j:
                if chars[i] not in vowels:
                    i += 1
                    continue
                if chars[j] not in vowels:
                    j -= 1
                    continue
                chars[i], chars[j] = chars[j], chars[i]
                i += 1
                j -= 1
            return ''.join(chars)
    
    ############
    
    import string
    
    
    class Solution(object):
      def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        vowels = set(["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"])
        s = list(s)
        start, end = 0, len(s) - 1
        while start < end:
          if s[start] not in vowels:
            start += 1
          elif s[end] not in vowels:
            end -= 1
          else:
            s[start], s[end] = s[end], s[start]
            start += 1
            end -= 1
        return "".join(s)
    
    

All Problems

All Solutions