aboutsummaryrefslogtreecommitdiff
path: root/min_change_alt_binary_str.c
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /min_change_alt_binary_str.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'min_change_alt_binary_str.c')
-rw-r--r--min_change_alt_binary_str.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/min_change_alt_binary_str.c b/min_change_alt_binary_str.c
new file mode 100644
index 0000000..52a763d
--- /dev/null
+++ b/min_change_alt_binary_str.c
@@ -0,0 +1,28 @@
+// 1758. Minimum Changes To Make Alternating Binary String
+#include "leetcode.h"
+
+/*
+ * given a string 's' consisting only of the characters '0' and '1', in one
+ * operation, you can change any '0' to '1' or vice versa. the string is called
+ * alternating if no two adjacent characters are equal. for example the string
+ * "010" is alternating, while the string "0100" is not. return the minimum
+ * number of operations needed to make 's' alternating.
+ */
+
+int minOperations(char *s) {
+ int n = strlen(s), cnt = 0;
+ for (int i = 0; i < n; i++) {
+ if (!(i % 2) && s[i] == '1')
+ cnt++;
+ else if (i % 2 && s[i] == '0')
+ cnt++;
+ }
+ return fmin(cnt, n - cnt);
+}
+
+int main() {
+ char *s1 = "0100", *s2 = "10", *s3 = "1111";
+ printf("%d\n", minOperations(s1)); // expect: 1
+ printf("%d\n", minOperations(s2)); // expect: 0
+ printf("%d\n", minOperations(s3)); // expect: 2
+}