761. Special Binary String LeetCode Solution

In this guide, you will get 761. Special Binary String LeetCode Solution with the best time and space complexity. The solution to Special Binary String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Special Binary String solution in C++
  4. Special Binary String solution in Java
  5. Special Binary String solution in Python
  6. Additional Resources
761. Special Binary String LeetCode Solution image

Problem Statement of Special Binary String

Special binary strings are binary strings with the following two properties:

The number of 0’s is equal to the number of 1’s.
Every prefix of the binary string has at least as many 1’s as 0’s.

You are given a special binary string s.
A move consists of choosing two consecutive, non-empty, special substrings of s, and swapping them. Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.
Return the lexicographically largest resulting string possible after applying the mentioned operations on the string.

Example 1:

Input: s = “11011000”
Output: “11100100”
Explanation: The strings “10” [occuring at s[1]] and “1100” [at s[3]] are swapped.
This is the lexicographically largest string possible after some number of swaps.

Example 2:

Input: s = “10”
Output: “10”

Constraints:

1 <= s.length <= 50
s[i] is either '0' or '1'.
s is a special binary string.

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(n)
See also  744. Find Smallest Letter Greater Than Target LeetCode Solution

761. Special Binary String LeetCode Solution in C++

class Solution {
 public:
  string makeLargestSpecial(string s) {
    vector<string> specials;
    int count = 0;

    for (int i = 0, j = 0; j < s.length(); ++j) {
      count += s[j] == '1' ? 1 : -1;
      if (count == 0) {  // Find a special string.
        const string& inner = s.substr(i + 1, j - i - 1);
        specials.push_back('1' + makeLargestSpecial(inner) + '0');
        i = j + 1;
      }
    }

    ranges::sort(specials, greater<>());
    return join(specials);
  }

 private:
  string join(const vector<string>& specials) {
    string joined;
    for (const string& special : specials)
      joined += special;
    return joined;
  }
};
/* code provided by PROGIEZ */

761. Special Binary String LeetCode Solution in Java

class Solution {
  public String makeLargestSpecial(String s) {
    List<String> specials = new ArrayList<>();
    int count = 0;

    for (int i = 0, j = 0; j < s.length(); ++j) {
      count += s.charAt(j) == '1' ? 1 : -1;
      if (count == 0) {
        specials.add("1" + makeLargestSpecial(s.substring(i + 1, j)) + "0");
        i = j + 1;
      }
    }

    Collections.sort(specials, Collections.reverseOrder());
    return String.join("", specials);
  }
}
// code provided by PROGIEZ

761. Special Binary String LeetCode Solution in Python

class Solution:
  def makeLargestSpecial(self, s: str) -> str:
    specials = []
    count = 0

    i = 0
    for j, c in enumerate(s):
      count += 1 if c == '1' else -1
      if count == 0:
        specials.append(
            '1' + self.makeLargestSpecial(s[i + 1:j]) + '0')
        i = j + 1

    return ''.join(sorted(specials)[::-1])
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.