345. Reverse Vowels of a String
https://leetcode.com/problems/reverse-vowels-of-a-string/
js
/**
* @param {string} s
* @return {string}
*/
var reverseVowels = function(s) {
const cs = s.split('')
const vowels = 'aeiouAEIOU'
let l = 0
let r = s.length - 1
while (l < r) {
if (vowels.indexOf(cs[l]) >= 0 && vowels.indexOf(cs[r]) >= 0) {
let temp = cs[r]
cs[r] = cs[l]
cs[l] = temp
l++
r--
} else if (vowels.indexOf(cs[l]) >= 0) {
r--
} else {
l++
}
}
return cs.join('')
}
py
class Solution:
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
vowels = 'aeiouAEIOU'
l = 0
r = len(s) - 1
cs = list(s)
while l < r:
if cs[l] in vowels and cs[r] in vowels:
cs[l], cs[r] = cs[r], cs[l]
l += 1
r -= 1
elif s[l] in vowels:
r -= 1
else:
l += 1
return ''.join(cs)