Skip to content

Commit

Permalink
Update 1702.Maximum-Binary-String-After-Change.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Dec 26, 2020
1 parent e7b5fde commit c068161
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@ class Solution {
public:
string maximumBinaryString(string binary)
{
int ones = 0, zeros = 0;
int n = binary.size();
int m = 0;
for (auto ch:binary)
{
if (ch=='1')
ones++;
else
zeros++;
if (ch=='0')
m++;
}
if (zeros <= 1)
return binary;
if (m<=1) return binary;

string ret;
int i = 0;
while (i<binary.size() && binary[i]=='1')
{
ret+="1";
ones--;
i++;
}

for (int i=0; i<zeros-1; i++)
}
for (int i=0; i<m-1; i++)
ret+="1";
ret+="0";
for (int i=0; i<ones; i++)
while (ret.size() < n)
ret+="1";

return ret;
Expand Down

0 comments on commit c068161

Please sign in to comment.