↓ 跳过正文
  1. 科学/
  2. 计算机/
  3. 算法/
  4. Leetcode/
  5. 0300-0399/

0345 反转字符串中的元音字母

Solution

from typing import str

class Solution:
    def reverseVowels(self, s: str) -> str:
        vowels = "aeiouAEIOU"
        i, j = 0, len(s) - 1
        cs = list(s)
        while i < j:
            while i < j and cs[i] not in vowels:
                i += 1
            while i < j and cs[j] not in vowels:
                j -= 1
            if i < j:
                cs[i], cs[j] = cs[j], cs[i]
                i, j = i + 1, j - 1
        return "".join(cs)

if __name__ == "__main__":
    sol = Solution()
    print(sol.reverseVowels("hello"))    # "holle"
    print(sol.reverseVowels("leetcode")) # "leotcede"
    print(sol.reverseVowels("aA"))       # "Aa"
    print(sol.reverseVowels("xyz"))      # "xyz"