Arena / Problems /

345. Reverse Vowels of a String

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #345

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

Example 1
Input: s = "IceCreAm"
Output: "AceCreIm"
Explanation:

The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".

Example 2
Input: "leetcode"
Output: "leotcede"
Constraints
  • 1 <= s.length <= 3 * 10^5
  • s consist of printable ASCII characters.
Topics & Algorithms
⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:

Use two pointers to swap vowels in the string. The left pointer finds vowels from the start, the right pointer finds vowels from the end, and they swap until the pointers meet.

class Solution:
    def reverseVowels(self, s: str) -> str:
        vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
        s = list(s)
        r = len(s) - 1

        for l in range(len(s)): # Important
            if s[l] not in vowels:
                continue

            while r > 0 and s[r] not in vowels:
                r -= 1

            if l >= r:
                break

            s[l], s[r] = s[r], s[l]
            r -= 1

        return ''.join(s)
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:

Use two pointers to swap vowels in the string. The left pointer finds vowels from the start, the right pointer finds vowels from the end, and they swap until the pointers meet.

class Solution {
    public String reverseVowels(String s) {
        var vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
        var chars = s.toCharArray();
        int r = chars.length - 1;

        for (int l = 0; l < chars.length; l++) { // important
            if (!vowels.contains(chars[l])) {
                continue;
            }

            while (r > 0 && !vowels.contains(chars[r])) {
                r--;
            }

            if (l >= r) {
                break;
            }

            // Swap the vowels
            char temp = chars[l];
            chars[l] = chars[r];
            chars[r] = temp;
            r--;
        }

        return new String(chars);
    }
}
⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C++ Solution by Anonymous Developer
0
Approach Intuition:

Use two pointers to swap vowels in the string. The left pointer finds vowels from the start, the right pointer finds vowels from the end, and they swap until the pointers meet.

class Solution {
public:
    string reverseVowels(string s) {
        unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        int r = s.size() - 1;

        for (int l = 0; l < s.size(); l++) { // important
            if (!vowels.contains(s[l])) {
                continue;
            }

            while (r > 0 && !vowels.contains(s[r])) {
                r--;
            }

            if (l >= r) {
                break;
            }

            swap(s[l], s[r]);
            r--;
        }

        return s;
    }
};
⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Javascript Solution by Anonymous Developer
0
Approach Intuition:

Use two pointers to swap vowels in the string. The left pointer finds vowels from the start, the right pointer finds vowels from the end, and they swap until the pointers meet.

var reverseVowels = function (s) {
  const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
  const chars = s.split('')
  let r = chars.length - 1

  for (let l = 0; l < chars.length; l++) { // important
    if (!vowels.has(chars[l])) {
      continue
    }

    while (r > 0 && !vowels.has(chars[r])) {
      r--
    }

    if (l >= r) {
      break
    }

    [chars[l], chars[r]] = [chars[r], chars[l]]
    r--
  }

  return chars.join('')
};
⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:

Use two pointers to swap vowels in the string. The left pointer finds vowels from the start, the right pointer finds vowels from the end, and they swap until the pointers meet.

def reverse_vowels(s)
  vowels = Set.new(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
  r = s.size - 1

  (0...s.size).each do |l|
    unless vowels.include?(s[l])
      next
    end

    while r > 0 && !vowels.include?(s[r])
      r -= 1
    end

    if l >= r
      break
    end

    s[l], s[r] = s[r], s[l]
    r -= 1
  end

  s
end
⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C# Solution by Anonymous Developer
0
Approach Intuition:

Use two pointers to swap vowels in the string. The left pointer finds vowels from the start, the right pointer finds vowels from the end, and they swap until the pointers meet.

public class Solution
{
    public string ReverseVowels(string s)
    {
        var vowels = new HashSet<char> {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        var chars = s.ToCharArray();
        int r = chars.Length - 1;
        // important
        for (int l = 0; l < chars.Length; l++)
        {
            if (!vowels.Contains(chars[l]))
            {
                continue;
            }

            while (r > 0 && !vowels.Contains(chars[r]))
            {
                r--;
            }

            if (l >= r)
            {
                break;
            }

            (chars[l], chars[r]) = (chars[r], chars[l]);
            r--;
        }

        return new string(chars);
    }
}
⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Go Solution by Anonymous Developer
0
Approach Intuition:

Use two pointers to swap vowels in the string. The left pointer finds vowels from the start, the right pointer finds vowels from the end, and they swap until the pointers meet.

func reverseVowels(s string) string {
    vowels := map[byte]bool{
        'a': true, 'e': true, 'i': true, 'o': true, 'u': true,
        'A': true, 'E': true, 'I': true, 'O': true, 'U': true,
    }
    chars := []byte(s)
    r := len(chars) - 1

    for l := 0; l < len(chars); l++ { // important
        if !vowels[chars[l]] {
            continue
        }

        for r > 0 && !vowels[chars[r]] {
            r--
        }

        if l >= r {
            break
        }

        chars[l], chars[r] = chars[r], chars[l]
        r--
    }

    return string(chars)
}
⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #345 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.