blob: 4d0ed57b493d45d232f647a7859ee3e969c7ab1b (
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
|
// 2696. Minimum String Length After Removing Substrings
#include "leetcode.h"
/*
* you are given a string 's' consisting only of uppercase english letters. you
* can apply some operations to this string where in one operation you can
* remove any occurence of one of the sbustrings 'AB' or 'CD' from 's'. return
* the minimum possible length of the resulting string that you can obtain. note
* that the string concatenates after removing the substring and could produce
* new 'AB' or 'CD' substrings
*/
int minLength(char *s) {
int n = strlen(s), top = -1, ans;
char *stack = (char *)malloc((n + 1) * sizeof(char));
for (int i = 0; i < n; i++) {
if (top >= 0 &&
(stack[top] == 'A' && s[i] == 'B' || stack[top] == 'C' && s[i] == 'D'))
top--;
else
stack[++top] = s[i];
}
stack[++top] = '\0';
ans = top;
free(stack);
return ans;
}
int main() {
char *s1 = "ABFCACDB", *s2 = "ACBBD";
printf("%d\n", minLength(s1)); // expect: 2
printf("%d\n", minLength(s2)); // expect: 5
}
|