Skip to content

Commit d3ed46c

Browse files
solves reverse vowels in a string in python
1 parent 81ebc2b commit d3ed46c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
| 339 | 🔒 [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum) | Easy | |
9696
| 342 | [Power of Four](https://leetcode.com/problems/power-of-four) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/PowerOf4.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/power_of_four.py) |
9797
| 344 | [Reverse A String](https://leetcode.com/problems/reverse-string) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ReverseString.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/reverse_a_string.py) |
98-
| 345 | [Reverse Vowels of A String](https://leetcode.com/problems/reverse-vowels-of-a-string) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ReverseVowelsOfString.java) |
98+
| 345 | [Reverse Vowels of A String](https://leetcode.com/problems/reverse-vowels-of-a-string) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ReverseVowelsOfString.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/reverse_vowels_of_a_string.py) |
9999
| 346 | [Moving Average From Data Stream](https://leetcode.com/problems/moving-average-from-data-stream) | Easy | |
100100
| 349 | [Intersection of 2 Arrays](https://leetcode.com/problems/intersection-of-two-arrays) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IntersectionOfTwoArrays.java) |
101101
| 350 | [Intersection of 2 Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IntersectionOfTwoArraysII.java) |

python/reverse_vowels_of_a_string.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import re
2+
3+
4+
class Solution:
5+
def __init__(self):
6+
self.vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
7+
8+
def is_vowel(self, character: str) -> bool:
9+
return character in self.vowels
10+
11+
def index_in_range(self, l: iter, index: int) -> bool:
12+
return 0 <= index < len(l)
13+
14+
# 2 pointer approach
15+
# def reverseVowels(self, s: str) -> str:
16+
# characters = [character for character in s]
17+
# left, right = 0, len(characters) - 1
18+
# while left < right:
19+
# while left < right and not self.is_vowel(s[left]):
20+
# left += 1
21+
# while left < right and not self.is_vowel(s[right]):
22+
# right -= 1
23+
# if left < right:
24+
# characters[left], characters[right] = characters[right], characters[left]
25+
# left += 1
26+
# right -= 1
27+
# return ''.join(characters)
28+
29+
# faster method
30+
def reverseVowels(self, s):
31+
vowels = re.findall('(?i)[aeiou]', s)
32+
return re.sub('(?i)[aeiou]', lambda m: vowels.pop(), s)
33+
34+
print(Solution().reverseVowels('aA'))

0 commit comments

Comments
 (0)