The Indian Engineer

Problem 2914 Minimum Number of Changes to Make Binary String Beautiful

Posted on 5 mins

String

Problem Statement

Link - Problem 2914

Question

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:

You can change any character in s to 0 or 1.

Return the minimum number of changes required to make the string s beautiful.

Example 1:

Input: s = "1001"
Output: 2
Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100".
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.

Example 2:

Input: s = "10"
Output: 1
Explanation: We change s[1] to 1 to get string "11".
It can be seen that the string "11" is beautiful because we can partition it into "11".
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.

Example 3:

Input: s = "0000"
Output: 0
Explanation: We don't need to make any changes as the string "0000" is beautiful already.

Constraints:

Solution

class Solution {
public:
    int minChanges(string s) {
        int changes = 0;
        int size = s.size();
        for(int i=0;i<size; i= i+2){
            if(s[i]!=s[i+1])
                changes++;
        }
        return changes;

    }
};

Complexity Analysis

| Algorithm                 | Time Complexity | Space Complexity |
| ------------------------- | --------------- | ---------------- |
| Modified linear traversal | O(n)            | O(1)             |

Explanation

Intial Thoughts

To approach this problem, we should first identify the constraints given: the string has an even length and each character is either ‘0’ or ‘1’. Then, consider what makes a string ‘beautiful’: it can be divided into substrings of even lengths where each substring contains only ‘0’s or only ‘1’s. We need to think about how to efficiently compare and possibly change characters in pairs to achieve this. Key points to consider are: the impact of changing a character, how to minimize changes, and ensuring the resulting string can be divided as specified.

Intuitive Analysis

Intuitively, solving this problem involves iteratively checking each pair of adjacent characters in the string. If a pair has different characters, we must change one to match the other to meet the ‘beautiful’ criteria. The provided solution code reflects this intuition by looping through the string two characters at a time and incrementing a ‘changes’ counter whenever a mismatch is found. Notably, this approach works because it ensures that each substring of length 2 can be considered ‘beautiful’ by itself, thereby making the entire string ‘beautiful’ when all such substrings are considered together. Key aspects of this analysis include recognizing the importance of pairs, understanding how changes affect the string’s beauty, and optimizing the process to minimize the number of changes needed.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

For any valid partition, since each part consists of an even number of the same characters, we can further partition each part into lengths of exactly 2.

After noticing the first hint, we can decompose the whole string into disjoint blocks of size 2 and find the minimum number of changes required to make those blocks beautiful.