Advertisement
1758. Minimum Changes To Make Alternating Binary String
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Approach
Count mismatches for '1010...' pattern; answer is min(cost, n-cost).
1758.cs
C#
// Approach: Count mismatches for '1010...' pattern; answer is min(cost, n-cost).
// Time: O(n) Space: O(1)
public 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[i] - '0') == i % 2)
++cost10;
}
int cost01 = s.Length - cost10; // the cost to make s "0101"
return Math.Min(cost10, cost01);
}
}Advertisement
Was this solution helpful?