aboutsummaryrefslogtreecommitdiff
path: root/sort_vowel_string.c
blob: 07c31a4717d30926fc92f07d06c44f076ab6bfdf (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 2785. Sort Vowels in a String
#include "leetcode.h"

/*
 * 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.
  */

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;
    return true;
  }
  return false;
}

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 main() {
  char s1[] = {"lEetcOde"}, s2[] = {"lYmpH"};
  printf("%s\n", sortVowels(s1)); // expect: lEOtcede
  printf("%s\n", sortVowels(s2)); // expect: lYmpH
}