{x}
blog image

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.

Solution Explanation for LeetCode 761: Special Binary String

This problem asks us to find the lexicographically largest string achievable by swapping consecutive, non-empty special substrings within a given special binary string. A special binary string is defined as a string where:

  1. The number of 0s equals the number of 1s.
  2. Every prefix has at least as many 1s as 0s.

The solution utilizes a recursive approach combined with sorting to achieve the lexicographically largest string.

Approach

The core idea is to recursively break down the input string into smaller special substrings. A special substring will always start with a '1' and end with a '0', with a balanced number of '1's and '0's in between. We identify these substrings, recursively process them (to ensure they are themselves lexicographically largest), and then sort them in descending order before concatenating them back together. This ensures the largest possible lexicographical string.

Algorithm

  1. Base Case: If the input string s is empty, return an empty string.

  2. Find Special Substrings: Iterate through the string, keeping track of a running count (cnt) of 1s and 0s. When cnt becomes 0, a special substring has been found. This substring starts at index j + 1 and ends at index i.

  3. Recursive Call: Recursively call the function makeLargestSpecial on the substring between the '1' and '0' to ensure that it is also the lexicographically largest possible version of itself.

  4. Construct and Sort: Create a list of special substrings (prepending a '1' and appending a '0' to each found substring). Sort this list in descending lexicographical order.

  5. Concatenate and Return: Concatenate the sorted substrings to form the final lexicographically largest string.

Time and Space Complexity Analysis

  • Time Complexity: O(N log N), where N is the length of the input string. The recursion depth is at most N/2 (since each recursive call roughly halves the input size), and the sorting step has a time complexity of O(N log N).

  • Space Complexity: O(N) due to the recursive call stack and the list used to store the special substrings. In the worst case, the recursion depth could be proportional to N and the substrings list could store up to N/2 substrings.

Code Implementation (Python)

class Solution:
    def makeLargestSpecial(self, s: str) -> str:
        if s == '':
            return ''
        ans = []
        cnt = 0
        i = j = 0
        while i < len(s):
            cnt += 1 if s[i] == '1' else -1
            if cnt == 0:
                ans.append('1' + self.makeLargestSpecial(s[j + 1 : i]) + '0')
                j = i + 1
            i += 1
        ans.sort(reverse=True)
        return ''.join(ans)

The code in other languages (Java, C++, Go) follows a very similar structure, implementing the algorithm described above. The key difference is in the handling of strings and lists/arrays, which varies between programming languages.