[leetcode]345. Reverse Vowels of a String

来源:互联网 发布:广东麻将app源码 编辑:程序博客网 时间:2024/06/02 05:37

345. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = “hello”, return “holle”.

Example 2:
Given s = “leetcode”, return “leotcede”.

利用正则表达式,找到元音字母放在list中,然后pop()出来替换。简单粗暴不失优美的python代码

class Solution(object):    def reverseVowels(self, s):        """        :type s: str        :rtype: str        """        vowels = re.findall('(?i)[aeiou]', s)        return re.sub('(?i)[aeiou]', lambda m:vowels.pop(), s)
0 0
原创粉丝点击