Special Binary String Problem


Description

LeetCode Problem 761.

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:

1
2
3
4
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:

1
2
Input: s = "10"
Output: "10"

Constraints:

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


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
    string makeLargestSpecial(string s) {
        // 1. split s into special substrings
        vector<string> specials; 
        int cnt = 0;
        for (char c : s) {
            if (cnt==0) 
                specials.push_back("");
            c=='1' ? ++cnt : --cnt;
            specials.back() += c;            
        }

        // 2. recursively convert each "special" to largest special string
        for (auto& special : specials)
            special = '1' + makeLargestSpecial(special.substr(1, special.size()-2)) + '0';
        
        // 3. sort special substrings in greater lexicographic order
        sort(specials.begin(), specials.end(), greater<string>());

        // 4. join to get largest lexicographic special string
        string res;
        for (auto& special : specials) 
            res += special;
        return res;
    }
};




Related Posts

String Without Aaa Or Bbb Problem

LeetCode 984. Given two integers a and b, return any...

Shifting Letters Problem

LeetCode 848. You are given a string s of lowercase...

Positions Of Large Groups Problem

LeetCode 830. In a string sof lowercase letters, these letters...

Orderly Queue Problem

LeetCode 899. You are given a string s and an...

Number Of Lines To Write String Problem

LeetCode 806. You are given a string s of lowercase...

Masking Personal Information Problem

LeetCode 831. You are given a personal information string s,...