[Leetcode] [JavaScript] [Python] Reverse vowels of a String

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.