[Leet Code] Determine if String Halves Are Alike

Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.
Input: s = "MerryChristmas"
Output: false
Input: s = "AbCdEfGh"
Output: true
class Solution(object):
def halvesAreAlike(self, s):
"""
:type s: str
:rtype: bool
"""
vowels = ["a","e", "i", "o", "u"]
first_half = 0
answer = 0
for i in range(len(s)):
if i == len(s) / 2:
first_half = answer
answer = 0
if s[i].lower() in vowels:
answer += 1
print answer
return first_half == answer

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Boyd

Matthew Boyd

Learning, and posting my findings!