The title
You are given a string s of even length. Divide it into two halves of the same length, the first half is a and the second half is B.
Two strings are similar only if they both contain the same number of vowels (‘a’, ‘e’, ‘I’, ‘O’, ‘u’, ‘a’, ‘e’, ‘I’, ‘o’, ‘u’). Note that s may contain both upper and lower case letters.
Return true if a and B are similar; Otherwise, return false.
Example 1: Input: s = "book" Output: true Explanation: A = "bo" and b = "OK". A has a vowel sound, and B has a vowel sound. So, a is similar to B. Example 2: Input: s = "textbook" Output: false Explanation: A = "text" and b = "book" A has 1 vowel sound and B has 2 vowels. Therefore, a and B are not similar. Note that the vowel o occurs twice in b, counted as two. Example 3: Input s = "MerryChristmas" Output: false Example 4: Input S = "AbCdEfGh" Output: trueCopy the code
Tip:
2 <= s.length <= 1000 S. length is an even number. S consists of uppercase and lowercase letters
Their thinking
class Solution: def halvesAreAlike(self, s: str) -> bool: leftStr, rightStr = s[:len(s)//2], s[len(s)//2:] from collections import Counter leftCount = Counter(leftStr) rightCount = Counter(rightStr) testDict = { 'a':0, 'e':0, 'i':0, 'o':0, 'u':0, 'A': 0, 'E': 0, 'I': 0, 'O': 0, 'U': LeftDictList = leftcount.keys () & testdict.keys () leftDict = dict() for I in leftDict: leftDict[i] = leftCount[i] # print(leftDict) rightDictList = rightCount.keys() & testDict.keys() rightDict = dict() for i in rightDictList: rightDict[i] = rightCount[i] # print(rightDict) return sum(leftDict.values()) == sum(rightDict.values()) if __name__ == '__main__': s = "textbook" s = "book" s = "AbCdEfGh" ret = Solution().halvesAreAlike(s) print(ret)Copy the code