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".
The vowels in the string are arranged in reverse order, and the vowels in English are a, E, I, O, and U, and contain the case.
Two pointers, one at the head and one at the tail, and the vowel on the exchange two pointers to the contents.
I've been learning Python recently and will try to practice it with Python.
Js:
1 /**2 * @param {string} s3 * @return {string}4 */5 varReversevowels =function(s) {6 varI, res = S.split ("), start = 0, end = s.length-1,7Avowels = [' A ', ' e ', ' I ', ' o ', ' u '];8 while(Start <end) {9 while(Start < end &&!)Isvowel (S[start]))Tenstart++; One while(Start < end &&!)Isvowel (S[end])) Aend--; - if(Start!==end) - swap (start, end); thestart++; -end--; - } - returnRes.join (' '); + - functionIsvowel (Char){ + if(Avowels.indexof (Char. toLowerCase ())!==-1){ A return true; at } - return false; - } - functionSwap (I, j) { - vartemp =Res[i]; -Res[i] =Res[j]; inRES[J] =temp; - } to};
Python:
1 classsolution (object):2 defReversevowels (self, s):3res =list (s)4vowels = ['a','e','I','o','u']5start = 0; end = Len (s)-16 whileStart <End:7 whileStart < end and(S[start].lower () not inchvowels):8Start + = 19 whileStart < end and(S[end].lower () not inchvowels):TenEnd-= 1 One if(Start! =end): ATMP =Res[start] -Res[start] =Res[end] -Res[end] =tmp theStart + = 1; End-= 1 - return "'. Join (RES)
[Leetcode] [JavaScript] [Python] Reverse vowels of a String