LeetCode #345 Reverse Vowels of a String Solution & Explanation

LeetCode Problem

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.


Solution

In our solution, we use two arrays to separately store the vowel characters and their indices.

Then we can reverse the vowel list and replace them with their indices.

We can use two for loop to solve this problem.

C# Solution

Solution1

public class Solution {
    public string ReverseVowels(string s) {
        char[] vowelList = { 'a', 'e', 'i', 'o', 'u','A', 'E', 'I', 'O', 'U' };
        List<char>list = new List<char>();
        List<int>idxList = new List<int>();

        char[] charArr = s.ToCharArray();

        for(int i=0; i<charArr.Count();i++)
        {
            var now = charArr[i];
            if(vowelList.Contains(now))
            {
                list.Add(now);
                idxList.Add(i);
            }
        }

        list.Reverse(); //LINQ

        for(int j=0; j<idxList.Count();j++)
        {
            charArr[idxList[j]] = list[j];
        }

        return new string(charArr);
    }
}

And here’s a little thing that we can improve in this solution.

We can improve the efficiency of the second for loop by reducing its count.

Solution2

public class Solution {
    public string ReverseVowels(string s) {
        char[] vowelList = { 'a', 'e', 'i', 'o', 'u','A', 'E', 'I', 'O', 'U' };
        List<char> list = new List<char>();
        List<int> idxList = new List<int>();

        char[] charArr = s.ToCharArray();

        for (int i = 0; i < charArr.Length; i++) {
            var now = charArr[i];
            if (vowelList.Contains(now)) {
                list.Add(now);
                idxList.Add(i);
            }
        }

        list.Reverse();

        for (int j = 0; j < idxList.Count / 2; j++) {
            charArr[idxList[j]] = list[j];
            charArr[idxList[idxList.Count-1-j]] = list[idxList.Count-1-j];
        }

        return new string(charArr);
    }
}

Java Solution

Solution1

class Solution {
    public String reverseVowels(String s) {
        List<Character> vowelList =  Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
        List<Character> list = new ArrayList<>();
        List<Integer> idxList = new ArrayList<>();

        char[] charArr = s.toCharArray();

        for (int i = 0; i < charArr.length; i++) {
            char now = charArr[i];
            if (vowelList.contains(now)) {
                list.add(now);
                idxList.add(i);
            }
        }

        Collections.reverse(list);

        for (int j = 0; j < idxList.size() / 2; j++) {
            charArr[idxList.get(j)] = list.get(j);
            charArr[idxList.get(idxList.size() - 1 - j)] = list.get(idxList.size() - 1 - j);
        }
        return new String(charArr);
    }
}

Change the vowel list to HashSet will be faster a little bit

Solution2

class Solution {
    public String reverseVowels(String s) {
        Set<Character> vowelList = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
        List<Character> list = new ArrayList<>();
        List<Integer> idxList = new ArrayList<>();

        char[] charArr = s.toCharArray();

        for (int i = 0; i < charArr.length; i++) {
            char now = charArr[i];
            if (vowelList.contains(now)) {
                list.add(now);
                idxList.add(i);
            }
        }

        Collections.reverse(list);

        for (int j = 0; j < idxList.size() / 2; j++) {
            charArr[idxList.get(j)] = list.get(j);
            charArr[idxList.get(idxList.size() - 1 - j)] = list.get(idxList.size() - 1 - j);
        }
        return new String(charArr);
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts or clicking ads, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Reverse Vowels of a String – LeetCode

Some Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *