blob: 5d181584471b3191457f137b41361bfd3bf0b610 (
plain)
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
|
// 1704. Determine if String Halves Are Alike
#include "leetcode.h"
/*
* 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.
*/
int check(char *s, int n) {
int ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] >= 'a') {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u')
ans++;
} else {
if (s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' ||
s[i] == 'U')
ans++;
}
}
return ans;
}
bool halvesAreAlike(char *s) {
int n = strlen(s);
return check(s, n / 2) == check(&s[n / 2], n / 2) ? 1 : 0;
}
int main() {
char s1[] = {"book"}, s2[] = {"textbook"};
printf("%d\n", halvesAreAlike(s1)); // expect: 1
printf("%d\n", halvesAreAlike(s2)); // expect: 0
}
|