logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
13. Roman to Integer14. Longest Common Prefix20. Valid Parentheses26. Remove Duplicates from Sorted Array35. Search Insert Position69. Sqrt(x)70. Climbing Stairs88. Merge Sorted Array (In-Place)104. Maximum Depth of Binary Tree118. Pascal's Triangle121. Best Time to Buy and Sell Stock205. Isomorphic Strings242. Valid Anagram283. Move Zeroes345. Reverse Vowels of a String605. Can Place Flowers704. Binary Search796. Rotate String1071. Greatest Common Divisor of Strings1299. Replace Elements with Greatest Element on Right Side1431. Kids With the Greatest Number of Candies1614. Maximum Nesting Depth of the Parentheses1752. Check if Array Is Sorted and Rotated1768. Merge Strings Alternately1903. Largest Odd Number in String2965. Find Missing and Repeated Values
Code Scratch/leetcode/easy

345. Reverse Vowels of a String

2 min read
On this pageIntuition
IntuitionApproachComplexityCode

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

Input: s = "IceCreAm"
Output: "AceCreIm"
Explanation: The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".

# Intuition

  • we can swap the vowels in the front and back

# Approach

  • use two pointer to track the vowel at the front and back
  • if both point to a vowel swap them
  • continue till the front and back pointer meet

# Complexity

Space ComplexityTime Complexity
O(n)\text{O}(n)O(n)O(n)\text{O}(n)O(n)
  • extra storage is used to store the string converted to char array
  • we iterate over the array only once

# Code

class Solution {
    /**
     * check if character is a vowel irrespective of case
     */
    boolean isVowel(char x) {
        return (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U' || x == 'a' || x == 'e' || x == 'i' || x == 'o'
                || x == 'u');
    }
 
    public String reverseVowels(String s) {
        char[] c = s.toCharArray();
        int size = s.length();
 
        if (size == 1)
            return s;
 
        for (int i = 0, j = size - 1; i < j;) {
            // move left pointer if its not pointing to vowel
            if (!isVowel(c[i]))
                i++;
 
            // move right pointer if its not pointing to vowel
            if (!isVowel(c[j]))
                j--;
 
            // swap if both pointing to vowel
            if (isVowel(c[i]) && isVowel(c[j])) {
                // swap
                char x = c[j];
                c[j] = c[i];
                c[i] = x;
 
                // move left & right pointer to next element
                i++;
                j--;
            }
        }
 
        return new String(c);
    }
}
←Previous283. Move ZeroesNext→605. Can Place Flowers

On this page

IntuitionApproachComplexityCode