Advertisement
2914. Minimum Number of Changes to Make Binary String Beautiful
EasyView on LeetCode
Time: O(n)
Space: O(1)
Approach
Scan even-indexed pairs; each mismatched pair costs 1 change.
2914.cs
C#
// Approach: Scan even-indexed pairs; each mismatched pair costs 1 change.
// Time: O(n) Space: O(1)
public class Solution
{
public int MinChanges(string s)
{
int ans = 0;
for (int i = 0; i + 1 < s.Length; i += 2)
{
if (s[i] != s[i + 1])
++ans;
}
return ans;
}
}Advertisement
Was this solution helpful?