-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathstring_halves_are_alike.cpp
53 lines (49 loc) · 1.14 KB
/
string_halves_are_alike.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Src : LeetCode
--------------
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.
*/
class Solution
{
public:
bool isVowel(char ch)
{
switch (ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return true;
default:
return false;
}
}
bool halvesAreAlike(string s)
{
int count1 = 0, count2 = 0;
int n = s.length();
int half = n / 2;
for (int i = 0; i < half; i++)
{
if (isVowel(s[i]))
count1++;
}
for (int i = half; i < n; i++)
{
if (isVowel(s[i]))
count2++;
}
return count1 == count2;
}
};