1529. Minimum Suffix Flips
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Problem Overview
Minimum Suffix Flips (Unknown) asks you to solve a structured algorithmic task. This is a common String / Greedy pattern in coding interviews. Greedily count each time the current state differs from target[i]; flip state on mismatch.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Greedily count each time the current state differs from target[i]; flip state on mismatch.
1529.cs
C#
// Approach: Greedily count each time the current state differs from target[i]; flip state on mismatch.
// Time: O(n) Space: O(1)
public class Solution
{
public int MinFlips(string target)
{
int ans = 0;
int state = 0;
foreach (char c in target)
{
if (c - '0' != state)
{
state = c - '0';
++ans;
}
}
return ans;
}
}Was this solution helpful?
Related Problems
- 44. Wildcard Matching(Hard)
- 179. Largest Number(Medium)
- 316. Remove Duplicate Letters(Medium)
- 678. Valid Parenthesis String(Medium)
- 763. Partition Labels(Medium)
- 921. Minimum Add to Make Parentheses Valid(Medium)