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

最新文章

  1. mvc项目controller重命名了,用原网页url访问不了了,怎么办?
  2. hdu Prime Ring Problem
  3. 黑马程序员——【Java基础】——集合框架
  4. 怎么用ABBYY将PDF转换为JPEG图像
  5. scribe日志分析工具安装
  6. UVa 1594 (Floyd判圈) Ducci Sequence
  7. Mysql数据库优化总结2
  8. STL库之单链表:forward_list
  9. 关于Spring配置 (Cannot find class [org.apache.commons.dbcp.BasicDataSource] 问题)
  10. Sans Serif 与 Serif 字体是什么意思?
  11. Linux:系统的基本优化
  12. 笔记:Maven 依赖及配置详解
  13. HTTP 内容编码,也就这 2 点需要知道 | 实用 HTTP
  14. ftp无法上传问题
  15. 潭州课堂25班:Ph201805201 django 项目 第三十二课 后台站点管理(课堂笔记)
  16. ABP框架系列之三十五:(MVC-Controllers-MVC控制器)
  17. Git相关二三事(git reflog 和彩色branch)【转】
  18. sublime 指定打开某文件的类型(text/plain)
  19. 维护直线的线段树:Bzoj1568,Bzoj3938(Uoj88)
  20. LeetCode——Maximum Subarray

热门文章

  1. netbeans 正则替换
  2. TCP/UDP 常用端口列表
  3. as3.0 嵌入字体的用法
  4. Android 二维码扫描/生成
  5. layui超链接追加tab选项卡必须手动刷新才出现问题
  6. [leetcode]5. Longest Palindromic Substring最长回文子串
  7. 20175234 《Java程序设计》第二周学习总结(二)
  8. abp Cannot access a disposed object. A common cause of this error is disposing
  9. vue组件之时间组件
  10. 微软URLRewriter.dll的url重写在目标框架.Net Framework2.0、4.0和应用程序池经典模式、集成模式下的配置