首页 技术 正文
技术 2022年11月14日
0 收藏 694 点赞 4,422 浏览 2804 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922