mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
bf94aff668
* add leetcode Determine if String Halves Are Alike * fix variable name Co-authored-by: David Leal <halfpacho@gmail.com>
39 lines
714 B
C
39 lines
714 B
C
bool isVowel(char chr){
|
|
switch(chr){
|
|
case 'a':
|
|
case 'e':
|
|
case 'i':
|
|
case 'o':
|
|
case 'u':
|
|
case 'A':
|
|
case 'E':
|
|
case 'I':
|
|
case 'O':
|
|
case 'U':
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Counting
|
|
// Runtime: O(n)
|
|
// Space: O(1)
|
|
bool halvesAreAlike(char * s){
|
|
int lenS = strlen(s);
|
|
int halfVowels = 0;
|
|
int currVowels = 0;
|
|
|
|
for (int i = 0; i < lenS; i++){
|
|
if (isVowel(s[i])){
|
|
currVowels++;
|
|
}
|
|
|
|
if (2 * (i + 1) == lenS){
|
|
halfVowels = currVowels;
|
|
}
|
|
}
|
|
|
|
return 2 * halfVowels == currVowels;
|
|
}
|