leetcode--翻转字符串中的元音

给你一个字符串s,仅反转字符串中的所有元音字母,并返回结果字符串。

示例 1:
输入:s = "hello"
输出:"holle"
示例 2:
输入:s = "leetcode"
输出:"leotcede"

# -*- coding:utf-8 -*-


def reverse_meta_symbol(a_str):
    left_ptr, right_ptr = 0, len(a_str)-1
    meta_symbol = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    while left_ptr < right_ptr:
        while left_ptr < right_ptr and a_str[left_ptr] not in meta_symbol:
            left_ptr += 1

        while left_ptr < right_ptr and a_str[right_ptr] not in meta_symbol:
            right_ptr -= 1

        if left_ptr < right_ptr:
            a_str[left_ptr], a_str[right_ptr] = a_str[right_ptr], a_str[left_ptr]
            left_ptr += 1
            right_ptr -= 1

    return a_str


a_str = ['h', 'e', 'l', 'l', 'o']
print(reverse_meta_symbol(a_str))

 

posted @ 2021-09-27 09:14  techPark  阅读(34)  评论(0)    收藏  举报