Welcome to Subscribe On Youtube
345. Reverse Vowels of a String
Description
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.
Solutions
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
-
class Solution { public String reverseVowels(String s) { boolean[] vowels = new boolean[128]; for (char c : "aeiouAEIOU".toCharArray()) { vowels[c] = true; } char[] cs = s.toCharArray(); int i = 0, j = cs.length - 1; while (i < j) { while (i < j && !vowels[cs[i]]) { ++i; } while (i < j && !vowels[cs[j]]) { --j; } if (i < j) { char t = cs[i]; cs[i] = cs[j]; cs[j] = t; ++i; --j; } } return String.valueOf(cs); } } ////// 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) { bool vowels[128]; memset(vowels, false, sizeof(vowels)); for (char c : "aeiouAEIOU") { vowels[c] = true; } int i = 0, j = s.size() - 1; while (i < j) { while (i < j && !vowels[s[i]]) { ++i; } while (i < j && !vowels[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) ############ class Solution: def reverseVowels(self, s: str) -> str: vowels = "aeiouAEIOU" # or, vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} i, j = 0, len(s) - 1 cs = list(s) while i < j: while i < j and cs[i] not in vowels: i += 1 while i < j and cs[j] not in vowels: j -= 1 if i < j: cs[i], cs[j] = cs[j], cs[i] i, j = i + 1, j - 1 return "".join(cs)
-
func reverseVowels(s string) string { vowels := [128]bool{} for _, c := range "aeiouAEIOU" { vowels[c] = true } cs := []byte(s) i, j := 0, len(cs)-1 for i < j { for i < j && !vowels[cs[i]] { i++ } for i < j && !vowels[cs[j]] { j-- } if i < j { cs[i], cs[j] = cs[j], cs[i] i, j = i+1, j-1 } } return string(cs) }
-
function reverseVowels(s: string): string { const vowels = new Set(['a', 'e', 'i', 'o', 'u']); const cs = s.split(''); for (let i = 0, j = cs.length - 1; i < j; ++i, --j) { while (i < j && !vowels.has(cs[i].toLowerCase())) { ++i; } while (i < j && !vowels.has(cs[j].toLowerCase())) { --j; } [cs[i], cs[j]] = [cs[j], cs[i]]; } return cs.join(''); }
-
impl Solution { pub fn reverse_vowels(s: String) -> String { let vowel = String::from("aeiouAEIOU"); let mut data: Vec<char> = s.chars().collect(); let (mut i, mut j) = (0, s.len() - 1); while i < j { while i < j && !vowel.contains(data[i]) { i += 1; } while i < j && !vowel.contains(data[j]) { j -= 1; } if i < j { data.swap(i, j); i += 1; j -= 1; } } data.iter().collect() } }