diff options
author | Jack Sangdahl <[email protected]> | 2024-07-29 18:41:50 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-07-29 18:41:50 -0600 |
commit | ce2daba66586d89e0222f6d3ec44cc3408875552 (patch) | |
tree | d32be98cd2b1868f447b914697d00eba6ac28191 /min_deletions_balance_str.c | |
parent | 21a4d93a51c45f6366afc59a908e101b9076482c (diff) | |
download | leetcode-ce2daba66586d89e0222f6d3ec44cc3408875552.tar.gz leetcode-ce2daba66586d89e0222f6d3ec44cc3408875552.zip |
1653. minimum deletions to make string balanced
Diffstat (limited to 'min_deletions_balance_str.c')
-rw-r--r-- | min_deletions_balance_str.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/min_deletions_balance_str.c b/min_deletions_balance_str.c new file mode 100644 index 0000000..6daeda1 --- /dev/null +++ b/min_deletions_balance_str.c @@ -0,0 +1,29 @@ +// 1653. Minimum Deletions to Make String Balanced +#include "leetcode.h" + +/* + * you are given a string 's' consisting of only characters 'a' and 'b'. you can + * delete any number of characters in 's' to make 's' balanced. 's' is balanced + * if there is no pair of indices '(i, j)' such that 'i < j' and 's[i] = b' and + * 's[j] = a'. return the minimum number of deletions needed to make 's' + * balanced. + */ + +int minimumDeletions(char *s) { + int cnt = 0, ans = 0; + for (int i = 0; s[i] != '\0'; i++) { + if (s[i] == 'b') { + cnt++; + } else if (cnt) { + ans++; + cnt--; + } + } + return ans; +} + +int main() { + char *s1 = "aababbab", *s2 = "bbaaaaabb"; + printf("%d\n", minimumDeletions(s1)); // expect: 2 + printf("%d\n", minimumDeletions(s2)); // expect: 2 +} |