Welcome to Subscribe On Youtube
Question
Formatted question description: https://leetcode.ca/all/345.html
Given a string s
, reverse only all the vowels in the string and return it.
The vowels are 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
, and they can appear in both lower and upper cases, more than once.
Example 1:
Input: s = "hello" Output: "holle"
Example 2:
Input: s = "leetcode" Output: "leotcede"
Constraints:
1 <= s.length <= 3 * 105
s
consist of printable ASCII characters.
Algorithm
It should be noted that capital letters are counted, so there are ten letters in total.
We write a isVowel
function to determine whether the current character is a vowel,
- If both sides are vowels, then we exchange,
- If the one on the left is not, move one place to the right,
- If the one on the right is not, move one place to the left
Code
-
public class Reverse_Vowels_of_a_String { class Solution { public String reverseVowels(String s) { char[] a = s.toCharArray(); int l = 0; // left int r = a.length - 1; // right while (l < r) { if (isVowel(a[l]) && isVowel(a[r])) { swap(a, l++, r--); } else if (isVowel(a[l])) { r--; } else { l++; } } return new String(a); } private boolean isVowel(char b) { // The letters A, E, I, O, and U are called vowels char c = Character.toLowerCase(b); return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } private void swap(char[] cs, int start, int end) { char temp = cs[start]; cs[start] = cs[end]; cs[end] = temp; } } } ############ class Solution { public String reverseVowels(String s) { Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); int i = 0, j = s.length() - 1; char[] chars = s.toCharArray(); while (i < j) { if (!vowels.contains(chars[i])) { ++i; continue; } if (!vowels.contains(chars[j])) { --j; continue; } char t = chars[i]; chars[i] = chars[j]; chars[j] = t; ++i; --j; } return new String(chars); } }
-
// OJ: https://leetcode.com/problems/reverse-vowels-of-a-string/ // Time: O(N) // Space: O(1) class Solution { private: bool isVowel(char c) { c = tolower(c); return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } public: string reverseVowels(string s) { int i = 0, j = s.size() - 1; while (i < j) { while (i < j && !isVowel(s[i])) ++i; while (i < j && !isVowel(s[j])) --j; if (i < j) swap(s[i++], s[j--]); } return s; } };
-
class Solution: def reverseVowels(self, s: str) -> str: vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} i, j = 0, len(s) - 1 chars = list(s) while i < j: if chars[i] not in vowels: i += 1 elif chars[j] not in vowels: j -= 1 else: chars[i], chars[j] = chars[j], chars[i] i += 1 j -= 1 return ''.join(chars) ############ import string class Solution(object): def reverseVowels(self, s): """ :type s: str :rtype: str """ vowels = set(["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]) s = list(s) start, end = 0, len(s) - 1 while start < end: if s[start] not in vowels: start += 1 elif s[end] not in vowels: end -= 1 else: s[start], s[end] = s[end], s[start] start += 1 end -= 1 return "".join(s)
-
func reverseVowels(s string) string { left, right := 0, len(s)-1 a := []byte(s) for left < right { for left < right && !isVowel(a[left]) { left++ } for left < right && !isVowel(a[right]) { right-- } if left != right && isVowel(a[left]) && isVowel(a[right]) { a[left], a[right] = a[right], a[left] left++ right-- } } return string(a) } func isVowel(b byte) bool { return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' || b == 'A' || b == 'E' || b == 'I' || b == 'O' || b == 'U' }