Reverse Vowel of a String.
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 cases.
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Constraints:
1 <= s.length <= 3 * 105
s
consist of printable ASCII characters.
Solution Approach ->
There are many brute-force approach but we can solve this problem in O(n) time complexity by using two-pointer approach.
We will take two pointer one will start from left and another one start from right. There are three condition we can have in any of the given string.
Case 1 : If both character are vowel then simply we will swap those vowels and increment first pointer and decrement last pointer.
Case 2 : if the charter located on first pointer is vowel and other located on last pointer is not then just increment the first pointer.
Case 3 : This is opposite case of case 2. In this case , you can decrement the last pointer by one.
Case 4: if both character located on first and last pointer are not not vowels then just increment first and decrement last pointer.
Please find below code implemenation of this problem in C#.
Thank you readers for reading my story. I hope this would be helpful for others who are in process of improving their skills and cracking coding interview.