diff options
author | Jack Sangdahl <[email protected]> | 2024-11-04 20:52:50 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-11-04 20:52:50 -0700 |
commit | 5954ff6233f867fe746bd049f023dab44f7f5d4c (patch) | |
tree | db677467286dd31456c497d60407af734f06c39e /min_num_change_binary_str.c | |
parent | 6c724702128b3ccdaed34a885daee80734da9de6 (diff) | |
download | leetcode-5954ff6233f867fe746bd049f023dab44f7f5d4c.tar.gz leetcode-5954ff6233f867fe746bd049f023dab44f7f5d4c.zip |
2914. minimum number of changes to make binary string beautiful
Diffstat (limited to 'min_num_change_binary_str.c')
-rw-r--r-- | min_num_change_binary_str.c | 25 |
1 files changed, 25 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 +} |