Skip to content

Commit a18d20f

Browse files
committed
Sync LeetCode submission Runtime - 24 ms (32.18%), Memory - 16.9 MB (87.14%)
1 parent 25bdbe9 commit a18d20f

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> having an even length.</p>
2+
3+
<p>A string is <strong>beautiful</strong> if it&#39;s possible to partition it into one or more substrings such that:</p>
4+
5+
<ul>
6+
<li>Each substring has an <strong>even length</strong>.</li>
7+
<li>Each substring contains <strong>only</strong> <code>1</code>&#39;s or <strong>only</strong> <code>0</code>&#39;s.</li>
8+
</ul>
9+
10+
<p>You can change any character in <code>s</code> to <code>0</code> or <code>1</code>.</p>
11+
12+
<p>Return <em>the <strong>minimum</strong> number of changes required to make the string </em><code>s</code> <em>beautiful</em>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> s = &quot;1001&quot;
19+
<strong>Output:</strong> 2
20+
<strong>Explanation:</strong> We change s[1] to 1 and s[3] to 0 to get string &quot;1100&quot;.
21+
It can be seen that the string &quot;1100&quot; is beautiful because we can partition it into &quot;11|00&quot;.
22+
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> s = &quot;10&quot;
29+
<strong>Output:</strong> 1
30+
<strong>Explanation:</strong> We change s[1] to 1 to get string &quot;11&quot;.
31+
It can be seen that the string &quot;11&quot; is beautiful because we can partition it into &quot;11&quot;.
32+
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
33+
</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> s = &quot;0000&quot;
39+
<strong>Output:</strong> 0
40+
<strong>Explanation:</strong> We don&#39;t need to make any changes as the string &quot;0000&quot; is beautiful already.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>2 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
48+
<li><code>s</code> has an even length.</li>
49+
<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
50+
</ul>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Approach 1: Greedy
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def minChanges(self, s: str) -> int:
8+
current_char = s[0]
9+
10+
consecutive_count = 0
11+
min_changes_reqd = 0
12+
13+
for char in s:
14+
# If current char matches the prev sequence
15+
if char == current_char:
16+
consecutive_count += 1
17+
continue
18+
19+
# If we have even count of chars, start a new seq
20+
if consecutive_count % 2 == 0:
21+
consecutive_count = 1
22+
23+
# If odd count, change the current char
24+
else:
25+
consecutive_count = 0
26+
min_changes_reqd += 1
27+
28+
# Update current char for next iteration
29+
current_char = char
30+
31+
return min_changes_reqd
32+

0 commit comments

Comments
 (0)