Skip to content

Instantly share code, notes, and snippets.

@mfaani
Created September 29, 2025 01:29
Show Gist options
  • Save mfaani/7a781f40c108f437c7196073854a61d1 to your computer and use it in GitHub Desktop.
Save mfaani/7a781f40c108f437c7196073854a61d1 to your computer and use it in GitHub Desktop.
#leetcode #string #array
class Solution {
let vowels: Set<Character> = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
func reverseVowels(_ s: String) -> String {
var foundIndices: [String.Index] = []
var ans = s
for i in s.indices {
if vowels.contains(s[i]) {
foundIndices.append(i)
}
}
var l: String.Index?
var r: String.Index?
while let l = l, let r = r, l < r, l < s.endIndex, r >= s.startIndex {
// HOW CAN I SWAP THE CHARACTERS AT DIFFERENT INDEXES?
// ABANDONING because `String.Index` doesn't swap nor it allows you to set a character for a given index. I suppose it's because you can't guarantee that whatever value you're replacing has the same character size...
s.formIndex(after:l)
s.formIndex(before:r)
}
return ans
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment