|
| 1 | +--- |
| 2 | +id: reverse-vowels-of-a-string |
| 3 | +title: Reverse Vowels of a String Solution |
| 4 | +sidebar_label: 0345 - Reverse Vowels of a String |
| 5 | +tags: |
| 6 | + - Reverse Vowels of a String |
| 7 | + - String |
| 8 | + - Two Pointers |
| 9 | + - LeetCode |
| 10 | + - JavaScript |
| 11 | + - TypeScript |
| 12 | +description: "This is a solution to the Reverse Vowels of a String problem on LeetCode." |
| 13 | +sidebar_position: 345 |
| 14 | +--- |
| 15 | + |
| 16 | +In this tutorial, we will solve the Reverse Vowels of a String problem using efficient string manipulation techniques. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, and C++. |
| 17 | + |
| 18 | +## Problem Description |
| 19 | + |
| 20 | +Given a string `s`, reverse only all the vowels in the string and return it. |
| 21 | + |
| 22 | +The vowels are `'a', 'e', 'i', 'o', 'u'`, and they can appear in both lower and upper cases, more than once. |
| 23 | + |
| 24 | +### Examples |
| 25 | + |
| 26 | +**Example 1:** |
| 27 | + |
| 28 | +```plaintext |
| 29 | +Input: s = "hello" |
| 30 | +Output: "holle" |
| 31 | +``` |
| 32 | + |
| 33 | +**Example 2:** |
| 34 | + |
| 35 | +```plaintext |
| 36 | +Input: s = "leetcode" |
| 37 | +Output: "leotcede" |
| 38 | +``` |
| 39 | + |
| 40 | +### Constraints |
| 41 | + |
| 42 | +- `1 <= s.length <= 3 * 10^5` |
| 43 | +- `s` consists of printable ASCII characters. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Solution for Reverse Vowels of a String |
| 48 | + |
| 49 | +### Intuition and Approach |
| 50 | + |
| 51 | +To solve this problem, we can use a two-pointer approach to efficiently reverse the vowels in the string. We will place one pointer at the beginning (`left`) and another at the end (`right`) of the string. We will move these pointers towards each other, swapping vowels when both pointers point to a vowel. |
| 52 | + |
| 53 | +<Tabs> |
| 54 | + <tabItem value="Two Pointers" label="Two Pointers"> |
| 55 | + |
| 56 | +### Approach: Two Pointers |
| 57 | + |
| 58 | +We can use two pointers to efficiently reverse the vowels in the string. This involves traversing the string from both ends and swapping vowels when encountered. |
| 59 | + |
| 60 | +#### Implementation |
| 61 | + |
| 62 | +```jsx live |
| 63 | +function reverseVowels(s) { |
| 64 | + const vowels = new Set('aeiouAEIOU'); |
| 65 | + const arr = s.split(''); |
| 66 | + let left = 0; |
| 67 | + let right = arr.length - 1; |
| 68 | + |
| 69 | + while (left < right) { |
| 70 | + if (!vowels.has(arr[left])) { |
| 71 | + left++; |
| 72 | + continue; |
| 73 | + } |
| 74 | + if (!vowels.has(arr[right])) { |
| 75 | + right--; |
| 76 | + continue; |
| 77 | + } |
| 78 | + [arr[left], arr[right]] = [arr[right], arr[left]]; |
| 79 | + left++; |
| 80 | + right--; |
| 81 | + } |
| 82 | + |
| 83 | + return arr.join(''); |
| 84 | +} |
| 85 | + |
| 86 | +const input = "hello"; |
| 87 | +const result = reverseVowels(input); |
| 88 | + |
| 89 | +return ( |
| 90 | + <div> |
| 91 | + <p> |
| 92 | + <b>Input:</b> {input} |
| 93 | + </p> |
| 94 | + <p> |
| 95 | + <b>Output:</b> {result} |
| 96 | + </p> |
| 97 | + </div> |
| 98 | +); |
| 99 | +``` |
| 100 | + |
| 101 | +#### Codes in Different Languages |
| 102 | + |
| 103 | +<Tabs> |
| 104 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 105 | + <SolutionAuthor name="@aryansh-patel"/> |
| 106 | + ```javascript |
| 107 | + function reverseVowels(s) { |
| 108 | + const vowels = new Set('aeiouAEIOU'); |
| 109 | + const arr = s.split(''); |
| 110 | + let left = 0; |
| 111 | + let right = arr.length - 1; |
| 112 | + |
| 113 | + while (left < right) { |
| 114 | + if (!vowels.has(arr[left])) { |
| 115 | + left++; |
| 116 | + continue; |
| 117 | + } |
| 118 | + if (!vowels.has(arr[right])) { |
| 119 | + right--; |
| 120 | + continue; |
| 121 | + } |
| 122 | + [arr[left], arr[right]] = [arr[right], arr[left]]; |
| 123 | + left++; |
| 124 | + right--; |
| 125 | + } |
| 126 | + |
| 127 | + return arr.join(''); |
| 128 | + } |
| 129 | + ``` |
| 130 | + |
| 131 | + </TabItem> |
| 132 | + <TabItem value="TypeScript" label="TypeScript"> |
| 133 | + <SolutionAuthor name="@aryansh-patel"/> |
| 134 | + ```typescript |
| 135 | + function reverseVowels(s: string): string { |
| 136 | + const vowels = new Set('aeiouAEIOU'); |
| 137 | + const arr = s.split(''); |
| 138 | + let left = 0; |
| 139 | + let right = arr.length - 1; |
| 140 | +
|
| 141 | + while (left < right) { |
| 142 | + if (!vowels.has(arr[left])) { |
| 143 | + left++; |
| 144 | + continue; |
| 145 | + } |
| 146 | + if (!vowels.has(arr[right])) { |
| 147 | + right--; |
| 148 | + continue; |
| 149 | + } |
| 150 | + [arr[left], arr[right]] = [arr[right], arr[left]]; |
| 151 | + left++; |
| 152 | + right--; |
| 153 | + } |
| 154 | +
|
| 155 | + return arr.join(''); |
| 156 | + } |
| 157 | + ``` |
| 158 | + |
| 159 | + </TabItem> |
| 160 | + <TabItem value="Python" label="Python"> |
| 161 | + <SolutionAuthor name="@aryansh-patel"/> |
| 162 | + ```python |
| 163 | + def reverseVowels(s: str) -> str: |
| 164 | + vowels = set('aeiouAEIOU') |
| 165 | + s = list(s) |
| 166 | + left, right = 0, len(s) - 1 |
| 167 | +
|
| 168 | + while left < right: |
| 169 | + if s[left] not in vowels: |
| 170 | + left += 1 |
| 171 | + continue |
| 172 | + if s[right] not in vowels: |
| 173 | + right -= 1 |
| 174 | + continue |
| 175 | + s[left], s[right] = s[right], s[left] |
| 176 | + left += 1 |
| 177 | + right -= 1 |
| 178 | +
|
| 179 | + return ''.join(s) |
| 180 | + ``` |
| 181 | + |
| 182 | + </TabItem> |
| 183 | + <TabItem value="Java" label="Java"> |
| 184 | + <SolutionAuthor name="@aryansh-patel"/> |
| 185 | + ```java |
| 186 | + class Solution { |
| 187 | + public String reverseVowels(String s) { |
| 188 | + Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); |
| 189 | + char[] arr = s.toCharArray(); |
| 190 | + int left = 0, right = arr.length - 1; |
| 191 | +
|
| 192 | + while (left < right) { |
| 193 | + if (!vowels.contains(arr[left])) { |
| 194 | + left++; |
| 195 | + continue; |
| 196 | + } |
| 197 | + if (!vowels.contains(arr[right])) { |
| 198 | + right--; |
| 199 | + continue; |
| 200 | + } |
| 201 | + char temp = arr[left]; |
| 202 | + arr[left] = arr[right]; |
| 203 | + arr[right] = temp; |
| 204 | + left++; |
| 205 | + right--; |
| 206 | + } |
| 207 | +
|
| 208 | + return new String(arr); |
| 209 | + } |
| 210 | + } |
| 211 | + ``` |
| 212 | + |
| 213 | + </TabItem> |
| 214 | + <TabItem value="C++" label="C++"> |
| 215 | + <SolutionAuthor name="@aryansh-patel"/> |
| 216 | + ```cpp |
| 217 | + class Solution { |
| 218 | + public: |
| 219 | + string reverseVowels(string s) { |
| 220 | + unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; |
| 221 | + int left = 0, right = s.size() - 1; |
| 222 | +
|
| 223 | + while (left < right) { |
| 224 | + if (vowels.find(s[left]) == vowels.end()) { |
| 225 | + left++; |
| 226 | + continue; |
| 227 | + } |
| 228 | + if (vowels.find(s[right]) == vowels.end()) { |
| 229 | + right--; |
| 230 | + continue; |
| 231 | + } |
| 232 | + swap(s[left], s[right]); |
| 233 | + left++; |
| 234 | + right--; |
| 235 | + } |
| 236 | +
|
| 237 | + return s; |
| 238 | + } |
| 239 | + }; |
| 240 | + ``` |
| 241 | + |
| 242 | + </TabItem> |
| 243 | +</Tabs> |
| 244 | + |
| 245 | +#### Complexity Analysis |
| 246 | + |
| 247 | +- **Time Complexity:** $O(n)$, where `n` is the length of the string. |
| 248 | +- **Space Complexity:** $O(1)$, as we are not using any additional space. |
| 249 | + |
| 250 | +- The time complexity is linear in terms of the length of the string. Each character is checked and potentially swapped once, leading to a time complexity of $O(n)$. |
| 251 | +- The space complexity is constant because we only use a few extra variables regardless of the string size. |
| 252 | + |
| 253 | +</tabItem> |
| 254 | +</Tabs> |
| 255 | + |
| 256 | +:::tip Note |
| 257 | +This solution efficiently reverses the vowels in the string while maintaining the order of non-vowel characters. |
| 258 | +::: |
| 259 | + |
| 260 | +--- |
| 261 | + |
| 262 | +## Video Explanation of Reverse Vowels of a String |
| 263 | + |
| 264 | +<Tabs> |
| 265 | + |
| 266 | + <TabItem value="en" label="English"> |
| 267 | + |
| 268 | + --- |
| 269 | + |
| 270 | + <Tabs> |
| 271 | + <TabItem value="javascript" label="JavaScript"> |
| 272 | + <LiteYouTubeEmbed |
| 273 | + id="5mFEAT-CmVw" |
| 274 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 275 | + title="Reverse Vowels of a String Problem Explanation | Reverse Vowels of a String Solution" |
| 276 | + poster="maxresdefault" |
| 277 | + webp |
| 278 | + /> |
| 279 | + </TabItem> |
| 280 | + |
| 281 | + <TabItem value="python" label="Python"> |
| 282 | + <LiteYouTubeEmbed |
| 283 | + id="LzpGyJ4ll64" |
| 284 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 285 | + title="Reverse Vowels of a String Problem Explanation | Reverse Vowels of a String Solution" |
| 286 | + poster="maxresdefault" |
| 287 | + webp |
| 288 | + /> |
| 289 | + </TabItem> |
| 290 | + <TabItem value="java" label="Java"> |
| 291 | + <LiteYouTubeEmbed |
| 292 | + id="PO4AXFzGEOU" |
| 293 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 294 | + title="Reverse Vowels of a String Problem Explanation | Reverse Vowels of a String Solution" |
| 295 | + poster="maxresdefault" |
| 296 | + webp |
| 297 | + /> |
| 298 | + </TabItem> |
| 299 | + </Tabs> |
| 300 | + |
| 301 | + </TabItem> |
| 302 | + |
| 303 | + <TabItem value="hi" label="Hindi"> |
| 304 | + <LiteYouTubeEmbed |
| 305 | + id="MjIWsEuIqEk" |
| 306 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 307 | + title="Reverse Vowels of a String Problem Explanation | Reverse Vowels of a String Solution" |
| 308 | + poster="maxresdefault" |
| 309 | + webp |
| 310 | + /> |
| 311 | + </TabItem> |
| 312 | + |
| 313 | +</Tabs> |
0 commit comments