Skip to content

Commit faf3177

Browse files
authored
Merge pull request #3746 from ImmidiSivani/leetcode-1529
solution added to 1529
2 parents 447019b + d057e04 commit faf3177

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
id: minimum-suffix-flips
3+
title: Minimum Suffix Flips
4+
sidebar_label: 1529-Minimum Suffix Flips
5+
tags:
6+
- Greedy
7+
- LeetCode
8+
- Java
9+
- Python
10+
- C++
11+
description: "This is a solution to the Minimum Suffix Flips problem on LeetCode."
12+
sidebar_position: 2
13+
---
14+
15+
## Problem Description
16+
17+
You are given a 0-indexed binary string `target` of length `n`. You have another binary string `s` of length `n` that is initially set to all zeros. You want to make `s` equal to `target`.
18+
19+
In one operation, you can pick an index `i` where `0 <= i < n` and flip all bits in the inclusive range `[i, n - 1]`. Flip means changing '0' to '1' and '1' to '0'.
20+
21+
Return the minimum number of operations needed to make `s` equal to `target`.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
27+
```
28+
Input: target = "10111"
29+
Output: 3
30+
Explanation: Initially, s = "00000".
31+
Choose index i = 2: "00000" -> "00111"
32+
Choose index i = 0: "00111" -> "11000"
33+
Choose index i = 1: "11000" -> "10111"
34+
We need at least 3 flip operations to form target.
35+
```
36+
37+
**Example 2:**
38+
39+
```
40+
Input: target = "101"
41+
Output: 3
42+
Explanation: Initially, s = "000".
43+
Choose index i = 0: "000" -> "111"
44+
Choose index i = 1: "111" -> "100"
45+
Choose index i = 2: "100" -> "101"
46+
We need at least 3 flip operations to form target.
47+
```
48+
49+
**Example 3:**
50+
51+
```
52+
Input: target = "00000"
53+
Output: 0
54+
Explanation: We do not need any operations since the initial s already equals target.
55+
```
56+
57+
### Constraints
58+
59+
- `n == target.length`
60+
- `1 <= n <= 10^5`
61+
- `target[i]` is either '0' or '1'.
62+
63+
---
64+
65+
## Solution for Minimum Number of Flips to Make Binary String Equal Problem
66+
67+
### Approach: Greedy
68+
69+
To solve the problem, we can use a greedy approach. We need to count how many times the value changes from '0' to '1' or from '1' to '0' in the target string. This count will give us the minimum number of flips required.
70+
71+
#### Code in Different Languages
72+
73+
<Tabs>
74+
<TabItem value="C++" label="C++" default>
75+
<SolutionAuthor name="@ImmidiSivani"/>
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int minFlips(string target) {
81+
int flips = 0;
82+
char current = '0';
83+
for (char c : target) {
84+
if (c != current) {
85+
flips++;
86+
current = c;
87+
}
88+
}
89+
return flips;
90+
}
91+
};
92+
```
93+
94+
</TabItem>
95+
<TabItem value="Java" label="Java">
96+
<SolutionAuthor name="@ImmidiSivani"/>
97+
98+
```java
99+
class Solution {
100+
public int minFlips(String target) {
101+
int flips = 0;
102+
char current = '0';
103+
for (char c : target.toCharArray()) {
104+
if (c != current) {
105+
flips++;
106+
current = c;
107+
}
108+
}
109+
return flips;
110+
}
111+
}
112+
```
113+
114+
</TabItem>
115+
<TabItem value="Python" label="Python">
116+
<SolutionAuthor name="@ImmidiSivani"/>
117+
118+
```python
119+
class Solution:
120+
def minFlips(self, target: str) -> int:
121+
flips = 0
122+
current = '0'
123+
for c in target:
124+
if c != current:
125+
flips += 1
126+
current = c
127+
return flips
128+
```
129+
130+
</TabItem>
131+
</Tabs>
132+
133+
#### Complexity Analysis
134+
135+
- **Time Complexity**: $O(n)$, where $n$ is the length of the target string. We traverse the target string once.
136+
- **Space Complexity**: $O(1)$. We use a constant amount of extra space for variables.
137+
138+
---
139+
140+
<h2>Authors:</h2>
141+
142+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
143+
{['ImmidiSivani'].map(username => (
144+
<Author key={username} username={username} />
145+
))}
146+
</div>

0 commit comments

Comments
 (0)