Skip to content

Commit 36036ac

Browse files
Create 1704.DetermineifStringHalvesAreAlike.py
1 parent 4541c2c commit 36036ac

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
You are given a string s of even length. Split this
3+
string into two halves of equal lengths, and let a be
4+
the first half and b be the second half.
5+
6+
Two strings are alike if they have the same number of
7+
vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U').
8+
Notice that s contains uppercase and lowercase letters.
9+
10+
Return true if a and b are alike. Otherwise, return false.
11+
12+
Example:
13+
Input: s = "book"
14+
Output: true
15+
Explanation: a = "bo" and b = "ok". a has 1 vowel and b
16+
has 1 vowel. Therefore, they are alike.
17+
18+
Example:
19+
Input: s = "textbook"
20+
Output: false
21+
Explanation: a = "text" and b = "book". a has 1 vowel
22+
whereas b has 2. Therefore, they are not
23+
alike.
24+
Notice that the vowel o is counted twice.
25+
26+
Example:
27+
Input: s = "MerryChristmas"
28+
Output: false
29+
30+
Example:
31+
Input: s = "AbCdEfGh"
32+
Output: true
33+
34+
Constraints:
35+
- 2 <= s.length <= 1000
36+
- s.length is even.
37+
- s consists of uppercase and lowercase letters.
38+
'''
39+
#Difficulty: Easy
40+
#113 / 113 test cases passed.
41+
#Runtime: 32 ms
42+
#Memory Usage: 14.4 MB
43+
44+
#Runtime: 32 ms, faster than 87.87% of Python3 online submissions for Determine if String Halves Are Alike.
45+
#Memory Usage: 14.4 MB, less than 40.73% of Python3 online submissions for Determine if String Halves Are Alike.
46+
47+
class Solution:
48+
def halvesAreAlike(self, s: str) -> bool:
49+
a = 0
50+
b = 0
51+
s = s.lower()
52+
vowels = 'aeiou'
53+
length = len(s)//2
54+
for char in vowels:
55+
a += s[:length].count(char)
56+
b += s[length:].count(char)
57+
return a == b

0 commit comments

Comments
 (0)