程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python solution of reversing vowel letters in string

編輯:Python

Give you a string  s , Invert only all vowels in the string , And return the result string .

Vowels include  'a''e''i''o''u', And may appear in both case .

example :

 Input :s = "hello"
 Output :"holle"

analysis :

Only vowels in a string are conditionally constrained , So we just need to store the vowels , Then you can reverse replace the vowels of the string , The first thing that comes to mind when you meet the condition of "first in, last out" is the stack .

class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
stack = [] # Create a stack
for i in s: # Storing vowels
if i in 'aeiouAEIOU':
stack.append(i)
res = '' # Result string
for j in s:
if j not in 'aeiouAEIOU': # Not a direct reproduction of vowels
res += j
else:
res += stack.pop() # If it is a vowel, it is reversed
return res


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved