解题思路
直接模拟。将字符串分为两半,分别遍历统计各元音出现的次数,最后比较是否相等即可。
核心代码如下:
bool halvesAreAlike(string s) {
string a = s.substr(0, s.size() / 2);
string b = s.substr(s.size() / 2);
string h = "aeiouAEIOU";
int sum1 = 0, sum2 = 0;
for (int i = 0; i < a.size(); i++) {
if (h.find_first_of(a[i]) != string::npos) {
sum1++;
}
}
for (int i = 0; i < b.size(); i++) {
if (h.find_first_of(b[i]) != string::npos) {
sum2++;
}
}
return sum1 == sum2;
}
标签:string,++,sum2,sum1,halves,int,alike,Determine,size
From: https://www.cnblogs.com/hql5/p/16880206.html