zoukankan      html  css  js  c++  java
  • 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 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

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Android属性动画
    android 保存配置文档
    android 不自动弹出虚拟键盘
    android 常用代码
    android imageswitcher gallery 根据数据库内图片名字进行查看/删除
    android 文件内容和 textview 操作
    用 java 语言获取 1N 的不重复随机数
    android 数据库 备份还原
    解决Ubuntu 输入法不显示
    android 添加文本内容到sqlite表中
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10776697.html
Copyright © 2011-2022 走看看