From 2769f3100712f1dc4f8c27181fc2c1f98fc3352c Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Wed, 10 Sep 2025 21:55:44 -0600 Subject: 2785. sort vowels in a string --- sort_vowel_string.c | 70 ++++++++++++++++++++-------------------------------- sort_vowel_string.py | 41 +++++++++++++----------------- 2 files changed, 44 insertions(+), 67 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() { diff --git a/sort_vowel_string.py b/sort_vowel_string.py index b0cce99..6ffdfee 100644 --- a/sort_vowel_string.py +++ b/sort_vowel_string.py @@ -1,16 +1,16 @@ # 2785. Sort Vowels in a String """ - given a 0-indexed string 's', permute 's' to get a new string 't'. - - All consonants remain in their original places. More formally, if there is - an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = - s[i]. - - The vowels must be sorted in the nondecreasing order of their ASCII - values. More formally, for pairs of indices i, j with 0 <= i < j < s.length - such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII - value than t[j]. - return the resulting string. consonants comprise all letter that are not - vowels. + given a 0-indexed string 's', permute 's' to get a new string 't'. + - All consonants remain in their original places. More formally, if there is +an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = +s[i]. +- The vowels must be sorted in the nondecreasing order of their ASCII +values. More formally, for pairs of indices i, j with 0 <= i < j < s.length +such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII +value than t[j]. + return the resulting string. consonants comprise all letter that are not +vowels. """ @@ -20,20 +20,13 @@ class Solution(object): :type s: str :rtype: str """ - vowel = [97, 101, 105, 111, 117, 65, 69, 73, 79, 85] - s = list(s) - lis = [] - idx = [] - for i in range(len(s)): - if ord(s[i]) in vowel: - lis.append(ord(s[i])) - idx.append(i) - lis.sort() - if len(lis) < 2: - return "".join(s) - for i in range(len(idx)): - s[idx[i]] = chr(lis[i]) - return "".join(s) + vowel_map = set("AEIOUaeiou") + vowels = [i for i in s if i in vowel_map] + vowels.sort() + ans, i = [], iter(vowels) + for j in s: + ans.append(next(i) if j in vowel_map else j) + return "".join(ans) if __name__ == "__main__": -- cgit v1.2.3