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.
How do I solve it
Breakdown:
Create a set of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O','U').
Turn the string s param into a list (a MutableList in Kotlin).
Initialize your start and end pointers.
In a while loop (pointer1 < pointer2), check if current character in the list (of indices pointer1 & pointer2).
If not, increment/decrement accordingly.
If so, swap the characters with their corresponding indices & increment + decrement your pointers.
Return your List using a .join to convert it back to a string.
What is this problem
How do I solve it
Breakdown:
set
of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O','U').s
param into a list (a MutableList in Kotlin).pointer1 < pointer2
), check if current character in the list (of indicespointer1 & pointer2
)..join
to convert it back to a string.Resources
How to Swap elements in Kotlin
Swap Method in java.util.Collections
Kotlin - Most idiomatic way to convert a List to a MutableList