Problem
You are given a string s
of even length. Split this string into two halves of equal lengths, and let a
be the first half and b
be the second half.
Two strings are alike if they have the same number of vowels ('a'
, 'e'
, 'i'
, 'o'
, 'u'
, 'A'
, 'E'
, 'I'
, 'O'
, 'U'
). Notice that s
contains uppercase and lowercase letters.
Return true
if a
and b
are alike. Otherwise, return false
.
Example 1:
1 | Input: s = "book" |
Example 2:
1 | Input: s = "textbook" |
Example 3:
1 | Input: s = "MerryChristmas" |
Example 4:
1 | Input: s = "AbCdEfGh" |
Constraints:
2 <= s.length <= 1000
s.length
is even.s
consists of uppercase and lowercase letters.
Analysis
题目给出一个字符串,从中间对半切开,要求我们判断前半部分和后半部分中包含的元音字母个数是否相同。
因为是前后两半对比,所以遍历一半就可以了,每个字符都判断下是否元音,前后两部分各自统计一下元音出现的次数,最后看看两者是否相等即可。
Solution
注意大小写的问题,
Code
1 | class Solution { |
Summary
这是一道非常简单的字符串遍历题目,没什么难点。这道题目的分享到这里,感谢你的支持!