345. Reverse Vowels of a String
Problem
345. Reverse Vowels of a String
Approach
Wrote two methods. After checking Gongshui Sanye's hints, I realized this can be solved with two pointers — check whether the characters at both ends are vowels. Two improved versions below.
Code
class Solution:
def reverseVowels(self, s: str) -> str:
vowels = 'aeiouAEIOU'
start = 0
end = len(s) - 1
while start < end:
while s[end] not in vowels and start < end:
end -= 1
while s[start] not in vowels and start < end:
start += 1
if s[start] in vowels and s[end] in vowels:
s[start], s[end] = s[end], s[start]
start += 1
end -= 1
return ''.join(s)class Solution:
def reverseVowels(self, s: str) -> str:
s = list(s)
vowels = 'aeiouAEIOU'
ans = []
for i in s:
if i in vowels:
ans.append(i)
a = ''
for i in range(len(s)):
if s[i] in vowels:
a += ans.pop()
else:
a += s[i]
return ''.join(a)贡献者
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0
3138. Minimum Length of Anagram Concatenation
LeetCode 3138. Minimum Length of Anagram Concatenation — using character frequency counting and GCD to determine whether a string is formed by concatenating anagrams. The core technique is enumerating possible segment lengths and verifying consistent character counts across segments. Suitable for algorithm job seekers working through string and math problems.
42. Trapping Rain Water
LeetCode 42. Trapping Rain Water — two-pointer approach using bucket theory, tracking left/right max heights to calculate trapped water per bar; for intermediate algorithm learners.