Skip to content

Commit 0b5b59f

Browse files
committed
[LeetCode Sync] Runtime - 9 ms (66.31%), Memory - 18.6 MB (48.81%)
1 parent dcd1182 commit 0b5b59f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p>
2+
3+
<p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">s = &quot;IceCreAm&quot;</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">&quot;AceCreIm&quot;</span></p>
12+
13+
<p><strong>Explanation:</strong></p>
14+
15+
<p>The vowels in <code>s</code> are <code>[&#39;I&#39;, &#39;e&#39;, &#39;e&#39;, &#39;A&#39;]</code>. On reversing the vowels, s becomes <code>&quot;AceCreIm&quot;</code>.</p>
16+
</div>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p>
22+
23+
<p><strong>Output:</strong> <span class="example-io">&quot;leotcede&quot;</span></p>
24+
</div>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
31+
<li><code>s</code> consist of <strong>printable ASCII</strong> characters.</li>
32+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def reverseVowels(self, s: str) -> str:
3+
vowels = "aeiou"
4+
i, j = 0, len(s) - 1
5+
words = list(s)
6+
7+
while j > i:
8+
while j > i and words[i].lower() not in vowels:
9+
i += 1
10+
while j > i and words[j].lower() not in vowels:
11+
j -= 1
12+
if j > i:
13+
words[i], words[j] = words[j], words[i]
14+
i, j = i + 1, j - 1
15+
16+
return "".join(words)

0 commit comments

Comments
 (0)