aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--min_num_change_binary_str.c25
-rw-r--r--min_num_change_binary_str.py29
2 files changed, 54 insertions, 0 deletions
diff --git a/min_num_change_binary_str.c b/min_num_change_binary_str.c
new file mode 100644
index 0000000..8452144
--- /dev/null
+++ b/min_num_change_binary_str.c
@@ -0,0 +1,25 @@
+// 2914. Minimum Number of Changes to Make Binary String Beautiful
+#include "leetcode.h"
+
+/*
+ * you are given a 0-indexed binary string 's' having an even length. a string
+ * is beautiful if it's possible to partition it into one or more substrings
+ * such that each substring has an even length and each substring contains only
+ * 1's or only 0's. you can change any character in 's' to a 0 or 1. return the
+ * minimum number of changes required to make the string 's' beautiful.
+ */
+
+int minChanges(char *s) {
+ int changes = 0;
+ for (int i = 0; i < strlen(s); i += 2)
+ if (s[i] != s[i + 1])
+ changes++;
+ return changes;
+}
+
+int main() {
+ char *s1 = "1001", *s2 = "10", *s3 = "0000";
+ printf("%d\n", minChanges(s1)); // expect: 2
+ printf("%d\n", minChanges(s2)); // expect: 1
+ printf("%d\n", minChanges(s3)); // expect: 0
+}
diff --git a/min_num_change_binary_str.py b/min_num_change_binary_str.py
new file mode 100644
index 0000000..20c4cf0
--- /dev/null
+++ b/min_num_change_binary_str.py
@@ -0,0 +1,29 @@
+# 2914. Minimum Number of Changes to Make Binary String Beautiful
+
+"""
+you are given a 0-indexed binary string 's' having an even length. a string
+is beautiful if it's possible to partition it into one or more substrings
+such that each substring has an even length and each substring contains only
+1's or only 0's. you can change any character in 's' to a 0 or 1. return the
+minimum number of changes required to make the string 's' beautiful.
+"""
+
+
+class Solution(object):
+ def minChanges(self, s):
+ """
+ :type s: str
+ :rtype: int
+ """
+ n, changes = len(s), 0
+ for i in range(0, n, 2):
+ if s[i] != s[i + 1]:
+ changes += 1
+ return changes
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minChanges(s="1001"))
+ print(obj.minChanges(s="10"))
+ print(obj.minChanges(s="0000"))