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.

Given a special 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.)

At the end of any number of moves, what is the lexicographically largest resulting string possible?

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.

Note:

  1. S has length at most 50.
  2. S is guaranteed to be a special binary string as defined above.

Approach #1: Recursion. [Java]

class Solution {
public String makeLargestSpecial(String S) {
int count = 0, i = 0;
List<String> res = new ArrayList<String>();
for (int j = 0; j < S.length(); ++j) {
if (S.charAt(j) == '1') count++;
else count--;
if (count == 0) {
res.add('1' + makeLargestSpecial(S.substring(i+1, j)) + '0');
i = j + 1;
}
}
Collections.sort(res, Collections.reverseOrder());
return String.join("", res);
}
}

  

Analysis:

We can solve this problem by 4 steps:

1. Split S into several special string (as many as possible).

2. Special string starts with 1 and ends with 0. Recursion on the middle part.

3. Sort all special strings in lexicographically largest order.

4. Join and output all strings.

Approach #2: DFS. [C++]

class Solution {
public:
string makeLargestSpecial(string S) {
int i = 0;
return dfs(S, i);
} private:
string dfs(string& s, int& i) {
string res;
vector<string> toks;
while (i < s.size() && res.empty()) {
if (s[i++] == '1') toks.push_back(dfs(s, i));
else res += "1";
}
bool prefix = res.size();
sort(toks.begin(), toks.end());
for (auto it = toks.rbegin(); it != toks.rend(); ++it)
res += *it;
if (prefix) res += '0';
return res;
}
};

  

Analysis:

If we map '1' to '(', '0' to ')', a Special-String is essentially Valid-Parentheses, therefore share all the properties of a Valid-Parenthese A VP (Valid-Parentheses) have 2 form:

Single nested VP like "(())", or "1100";

a number of consecutive sub-VPs like "()(())", or "101100", which contains "()" + "(())" or "10" + "1100"

And this problem is essentially ask you to reorder the sub-VPs in a VP to make it bigger. If we look at this example: "()(())" or "101100", how would you make it bigger?

Answer is, by moving the 2nd sub-string to the front. Because deeply nested VP contains more consecutive '('s or '1's in the front. That will make reordered string bigger.

The above example is straintforward, and no recursion is needed. But, what is the groups of sub-VPs are not in the root level?

Like if we put need to recurively reorder the children, make them MVP(Max-Valid-Parentheses), then reorder in root.

To summarize, we just need to reorder all group of VPs or SS's at each level to make them MVP, then reorder higher level VPs.

Reference:

https://leetcode.com/problems/special-binary-string/discuss/113212/Think-of-it-as-Valid-Parentheses

https://leetcode.com/problems/special-binary-string/discuss/113211/JavaC%2B%2BPython-Easy-and-Concise-Recursion

761. Special Binary String的更多相关文章

  1. leetcode 761. Special Binary String

    761. Special Binary String 题意: 一个符合以下两个要求的二进制串: \(1.串中包含的1和0的个数是相等的.\) \(2.二进制串的所有前缀中1的个数不少于0的个数\) 被 ...

  2. 【LeetCode】761. Special Binary String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...

  3. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  4. [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  5. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  6. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  7. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  8. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  9. encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.

    use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' => 'Ken' , 'age' => 1 ...

随机推荐

  1. django 开发之前后端分离开发模式

    1. 什么是前后端分离开发的概念: 前端页面运行前端服务器上,负责页面的渲染(静态文件的加载)与跳转 后端代码运行在后端服务器上, 负责数据的处理(提供数据请求的接口) 2. 前后端分离开发碰到的问题 ...

  2. python多线程简单爬虫

    爬虫本质就是将网站或者接口的数据经过筛选后按需求保存 这里实现一个简单爬虫仅供参考 import requests import bs4 import threading import queue i ...

  3. 关于如何安装使用Git、tortoiseGit、Git@osc

    摘要: 讲解git在git@osc上使用的正确入门姿势. 关于Git代码托管的好处,这里就不再进行说明了.相信想去使用的人都应该有所了解啦.在使用开源中国里面的git@osc时,我们得先做入下几个工作 ...

  4. EnterpriseLibrary

    收藏一下: http://www.cnblogs.com/huangcong/archive/2010/06/01/1748672.html

  5. C++ 获取字符串中的所有汉字

    #include<iostream> using namespace std; int main() {    char str[20] = "cd大家好df";   ...

  6. django模板总结

    1. 加载静态文件 html顶部:{% load staticfiles %} 调用: <link rel="stylesheet" type="text/css& ...

  7. python中的字符串

    一.在python中,字符串是不可变类型 通过以下代码说明: >>> s = 'hello, world' >>> id(s) 2108634288304 > ...

  8. python数据类型分类

    python数据分为可变类型和不可变类型,其中:可变类型:列表,字典不可变类型:布尔值,数字,字符串,元组 specidal:集合作为set是可变的,而作为frozenset是不可变集合 可变类型和不 ...

  9. FortiGate防火墙HA下联堆叠交换机

    1.拓扑图 2.防火墙配置 3.交换机配置 interface GigabitEthernet1/0/47 switchport access vlan 30 switchport mode acce ...

  10. 20165315 2017-2018-2《Java程序设计》课程总结

    20165315 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安 ...