diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /remove_coloured_pc.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'remove_coloured_pc.c')
-rw-r--r-- | remove_coloured_pc.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/remove_coloured_pc.c b/remove_coloured_pc.c new file mode 100644 index 0000000..c597d72 --- /dev/null +++ b/remove_coloured_pc.c @@ -0,0 +1,36 @@ +// 2038. Remove Colored Pieces if Both Neighbors are the Same Color +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +bool winnerOfGame(char *colours) { + int n = strlen(colours); + int grpa = 0, grpb = 0, cnt = 1; + for (int i = 1; i < n; i++) { + if (colours[i] == colours[i - 1]) + cnt++; + else { + if (cnt > 2) { + if (colours[i - 1] == 'A') + grpa += cnt - 2; + else + grpb += cnt - 2; + } + cnt = 1; + } + } + if (cnt > 2) { + if (colours[n - 1] == 'A') + grpa += cnt - 2; + else + grpb += cnt - 2; + } + return grpa > grpb; +} + +int main() { + char c1[] = {"AAABABB"}, c2[] = {"AA"}, c3[] = {"ABBBBBBBAAA"}; + printf("%d\n", winnerOfGame(c1)); // expect: 1 + printf("%d\n", winnerOfGame(c2)); // expect: 0 + printf("%d\n", winnerOfGame(c3)); // expect: 0 +} |