- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a binary string. We can apply each of the following operations any number of times −

If the number contains a substring "00", we can replace it with "10".

If the number contains a substring "10", we can replace it with "01".

Then we have to find the maximum binary (based on its numeric value) string we can get after any number of operations.

So, if the input is like s = "001100", then the output will be 111011, because we can transfer them like (00)1100 -> 101(10)0 -> 1010(10) -> 10(10)01 -> 100(10)1 -> 1(00)011 -> 111011.

To solve this, we will follow these steps −

- length := size of s
- zeros := number of 0s in s
- if zeros < 2, then
- return s

- s := remove all '1's from left of s
- leading_ones := length - size of s
- leading_ones := leading_ones + zeros - 1
- trailing_ones := length - leading_ones - 1
- answer_left := leading_ones number of 1s
- answer_right := trailing_ones number of 1s
- answer_left concatenate 0 concatenate answer_right and return

Let us see the following implementation to get better understanding −

def solve(s): length = len(s) zeros = s.count('0') if zeros < 2: return s s = s.lstrip('1') leading_ones = length - len(s) leading_ones += zeros - 1 trailing_ones = length - leading_ones - 1 answer_left = '1' * leading_ones answer_right = '1' * trailing_ones return ''.join([answer_left, '0', answer_right]) s = "001100" print(solve(s))

"001100"

111011

- Related Questions & Answers
- Program to find maximum element after decreasing and rearranging in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find string after removing consecutive duplicate characters in Python
- Program to find lexicographically smallest string after applying operations in Python
- Program to find the maximum width of a binary tree in Python
- Program to find shortest string after removing different adjacent bits in Python
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find length of longest substring with 1s in a binary string after one 0-flip in Python
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to find minimum changes required for alternating binary string in Python
- Program to find Kth bit in n-th binary string using Python
- Program to find minimum length of string after deleting similar ends in Python
- Program to find maximum score by splitting binary strings into two parts in Python
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find out the sum of the maximum subarray after a operation in Python

Advertisements