aboutsummaryrefslogtreecommitdiff
path: root/remove_dupplicate_letters.c
blob: f1dc4efd425fcd7725af357b5dece541fac140fe (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
// 316. Remove Duplicate Letters
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * given a string 's', remove duplicate letters so that every letter appears
 * once and only once. you must make sure your result is the smallest in
 * lexicographical order among all possible results.
 */

char *removeDuplicateLetters(char *s) {
  int n = strlen(s), a_idx = 0;
  int *digit = calloc(26, sizeof(int));
  for (int i = 0; i < n; i++)
    digit[s[i] - 'a']++;
  bool *set = calloc(26, sizeof(bool));
  char *ans = malloc(n + 1);
  for (int i = 0; i < n; i++) {
    if (set[s[i] - 'a']) {
      digit[s[i] - 'a']--;
      continue;
    }
    while (a_idx && s[i] < ans[a_idx] && digit[ans[a_idx - 1] - 'a']) {
      set[ans[a_idx - 1] - 'a'] = false;
      a_idx--;
    }
    ans[a_idx] = s[i];
    a_idx++;
    digit[s[i] - 'a']--;
    set[s[i] - 'a'] = true;
  }
  ans[a_idx] = '\0';
  return ans;
}

int main() {
  char s1[] = {"bcabc"}, s2[] = {"cbacdcbc"};
  printf("%s\n", removeDuplicateLetters(s1)); // expect: abc
  printf("%s\n", removeDuplicateLetters(s2)); // expect: acdb
}