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 — solution by enumerating substring lengths and using a hash map to verify whether the string can be formed by concatenating anagrams. The core technique is using character frequency arrays to check whether the substring's repeat multiplier matches the original string. Suitable for job seekers preparing for coding assessments and interviews who want to strengthen skills in string and counting algorithms.
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.