https://leetcode.com/problems/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".
Note:
The vowels does not include the letter "Y".
Maintain the first two pointers I and J, each cycle if s[i] and s[j] are vowel only exchange, otherwise which is not vowel, let i++ or j--, until the I==J traversal completed, Time complexity O (N)
It is important to note that in Python, str is not directly interchangeable with elements, so it needs to be converted to a list and then join to an empty string to return
Class solution (Object): def reversevowels (self, s): res = list (s) vowels = [' a ', ' e ', ' I ', ' o ', ' u '] i = 0 ; j = Len (s)-1 while i < j: if Res[i].lower () No in vowels: i + = 1 elif res[j].lower () not in vowels:< C8/>j-= 1 else: res[i],res[j] = res[j],res[i] i + = 1 J- = 1 return "". Join (RES)
Check if it is vowel can create a list of vowel, and then see if in can, no longer define a function to judge vowel
Leetcode #345 Reverse vowels of a String