diff options
author | Jack Sangdahl <[email protected]> | 2025-09-10 21:55:44 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-09-10 21:55:44 -0600 |
commit | 2769f3100712f1dc4f8c27181fc2c1f98fc3352c (patch) | |
tree | 4789073705acb0e2c8f5d2474d82d83adbcde13c /sort_vowel_string.c | |
parent | a675ae1a71ab3e88196a2a72a62f2c7a0d4c596a (diff) | |
download | leetcode-2769f3100712f1dc4f8c27181fc2c1f98fc3352c.tar.gz leetcode-2769f3100712f1dc4f8c27181fc2c1f98fc3352c.zip |
2785. sort vowels in a string
Diffstat (limited to 'sort_vowel_string.c')
-rw-r--r-- | sort_vowel_string.c | 70 |
1 files changed, 27 insertions, 43 deletions
diff --git a/sort_vowel_string.c b/sort_vowel_string.c index 07c31a4..6b5b929 100644 --- a/sort_vowel_string.c +++ b/sort_vowel_string.c @@ -14,54 +14,38 @@ vowels. */ -char map[10] = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'}; - -char is_vowel(char c, int *idx) { - if (c == 'A' || c == 'a') - return true; - else if (c == 'E' || c == 'e') { - (*idx) = 1; - return true; - } else if (c == 'I' || c == 'i') { - (*idx) = 2; - return true; - } else if (c == 'O' || c == 'o') { - (*idx) = 3; - return true; - } else if (c == 'U' || c == 'u') { - (*idx) = 4; +bool is_vowel(char c) { + switch (c) { + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': return true; + default: + return false; } - return false; } +int cmp(const void *a, const void *b) { return (*(char *)a - *(char *)b); } + char *sortVowels(char *s) { - int hash[10] = {0}, len = strlen(s); - char *ans = (char *)calloc(len + 1, sizeof(char)); - for (int i = 0; i < len; i++) { - int idx = 0; - if (is_vowel(s[i], &idx)) { - if (isupper(s[i])) - hash[idx]++; - else - hash[idx + 5]++; - } - } - int a_idx = 0, tmp; - for (int i = 0; i < len; i++) { - if (is_vowel(s[i], &tmp)) { - for (int j = 0; j < 10; j++) { - if (hash[j]) { - ans[a_idx++] = map[j]; - hash[j]--; - break; - } - } - } else { - ans[a_idx++] = s[i]; - } - } - return ans; + int n = strlen(s), cnt = 0; + char *vowels = (char *)malloc(n * sizeof(char)); + for (int i = 0; i < n; ++i) + if (is_vowel(s[i])) + vowels[cnt++] = s[i]; + qsort(vowels, cnt, sizeof(char), cmp); + for (int i = 0, j = 0; j < n; ++i) + if (is_vowel(s[i])) + s[i] = vowels[j++]; + free(vowels); + return s; } int main() { |