761. 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.
Given a special string
S, a move consists of choosing two consecutive, non-empty, special substrings ofS, 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:
Shas length at most50.Sis 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
761. Special Binary String的更多相关文章
- leetcode 761. Special Binary String
761. Special Binary String 题意: 一个符合以下两个要求的二进制串: \(1.串中包含的1和0的个数是相等的.\) \(2.二进制串的所有前缀中1的个数不少于0的个数\) 被 ...
- 【LeetCode】761. Special Binary String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.
use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' => 'Ken' , 'age' => 1 ...
随机推荐
- python——vs2017安装python库时,提示pip指令问题。
需要跟新pip指令 方法: 第一步:打开vs2017 后新建一个python 文件 后面如图 点击红色部分 2.在框中输入pip之后更新即可 如图 3.问题解决 倘若还有问题 欢迎分享
- Balls(扔鸡蛋问题)
4554 BallsThe classic Two Glass Balls brain-teaser is often posed as:“Given two identical glass sphe ...
- Asp.net之Sql注入与Parameter对象
一.Sql注入实例 using System; using System.Collections.Generic; using System.Data; using System.Data.SqlCl ...
- Match-----Correlation-----find_ncc_model_exposure
* This example program shows how to use HALCON's correlation-based* matching. In particular it demon ...
- 移动端过禁止输入emoji表情实现方案
最近手头上的项目有一个需求就是输入框不能输入表情,然后就各种在网上找资料,网上好多人给的方案是: str = str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uD ...
- cobbler的ks文件带有特殊字符导致错误
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAykAAACWAQMAAADg0FUUAAAAAXNSR0IArs4c6QAAAAZQTFRFq6urAA
- django 表单使用
Django提供对表单处理的支持,可以简化并自动化大部分的表单处理工作. 1 定义表单类 表单系统的核心部分是Django 的Form类. Django 的数据库模型描述一个对象的逻辑结构.行为以及展 ...
- SQL Server 定价及授权方式
https://www.microsoft.com/zh-cn/sql-server/sql-server-2017-pricing http://www.360doc.com/content/15/ ...
- 微擎开发------day04
(1) cache_write($key,$data) 按照指定键名缓存数据 cache_write('test', $data) (2) cache_load($key) 读取指定键名的缓存数据 ...
- Python - 最大公约数算法
# Python 3.6 # 最大公约数,最大公因子 # Greatest Common Divisor # 辗转相除法 def gcd(num1: object, num2: object) -&g ...