Skip to content

Commit 7a44860

Browse files
authored
Create determine-if-string-halves-are-alike.py
1 parent 286fdcd commit 7a44860

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def halvesAreAlike(self, s):
6+
"""
7+
:type s: str
8+
:rtype: bool
9+
"""
10+
vowels = set("aeiouAEIOU")
11+
cnt1 = cnt2 = 0
12+
left, right = 0, len(s)-1
13+
while left < right:
14+
cnt1 += s[left] in vowels
15+
cnt2 += s[right] in vowels
16+
left += 1
17+
right -= 1
18+
return cnt1 == cnt2

0 commit comments

Comments
 (0)