1758. Minimum Changes To Make Alternating Binary String LeetCode Solution

In this guide, you will get 1758. Minimum Changes To Make Alternating Binary String LeetCode Solution with the best time and space complexity. The solution to Minimum Changes To Make Alternating Binary String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Changes To Make Alternating Binary String solution in C++
  4. Minimum Changes To Make Alternating Binary String solution in Java
  5. Minimum Changes To Make Alternating Binary String solution in Python
  6. Additional Resources
1758. Minimum Changes To Make Alternating Binary String LeetCode Solution image

Problem Statement of Minimum Changes To Make Alternating Binary String

You are 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.

Example 1:

Input: s = “0100”
Output: 1
Explanation: If you change the last character to ‘1’, s will be “0101”, which is alternating.

Example 2:

Input: s = “10”
Output: 0
Explanation: s is already alternating.

Example 3:

Input: s = “1111”
Output: 2
Explanation: You need two operations to reach “0101” or “1010”.

Constraints:

1 <= s.length <= 104
s[i] is either '0' or '1'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1758. Minimum Changes To Make Alternating Binary String LeetCode Solution in C++

class Solution {
 public:
  int minOperations(string s) {
    int cost10;  // the cost to make s "1010"

    for (int i = 0; i < s.length(); ++i)
      if (s[i] - '0' == i % 2)
        ++cost10;

    const int cost01 = s.length() - cost10;  // the cost to make s "0101"
    return min(cost10, cost01);
  }
};
/* code provided by PROGIEZ */

1758. Minimum Changes To Make Alternating Binary String LeetCode Solution in Java

class Solution {
  public int minOperations(String s) {
    int cost10 = 0; // the cost to make s "1010"

    for (int i = 0; i < s.length(); ++i)
      if (s.charAt(i) - '0' == i % 2)
        ++cost10;

    final int cost01 = s.length() - cost10; // the cost to make s "0101"
    return Math.min(cost10, cost01);
  }
}
// code provided by PROGIEZ

1758. Minimum Changes To Make Alternating Binary String LeetCode Solution in Python

class Solution:
  def minOperations(self, s: str) -> int:
    # the cost to make s "1010"
    cost10 = sum(int(c) == i % 2 for i, c in enumerate(s))
    # the cost to make s "0101"
    cost01 = len(s) - cost10
    return min(cost10, cost01)
# code by PROGIEZ

Additional Resources

See also  3441. Minimum Cost Good Caption LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.