carsonSgit / leetcode

leetcode grind... doing whatever I can to make sure I'm not bad at CS.
4 stars 0 forks source link

Q345 #45

Closed carsonSgit closed 3 months ago

carsonSgit commented 3 months ago

What is this 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.

How do I solve it

Breakdown:

  1. Create a set of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O','U').
  2. Turn the string s param into a list (a MutableList in Kotlin).
  3. Initialize your start and end pointers.
  4. 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.
  5. Return your List using a .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