返回介绍

solution / 0700-0799 / 0761.Special Binary String / README_EN

发布于 2024-06-17 01:03:34 字数 3950 浏览 0 评论 0 收藏 0

761. Special Binary String

中文文档

Description

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.

You are given a special binary 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.

Return _the lexicographically largest resulting string possible after applying the mentioned operations on the string_.

 

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.

Example 2:

Input: s = "10"
Output: "10"

 

Constraints:

  • 1 <= s.length <= 50
  • s[i] is either '0' or '1'.
  • s is a special binary string.

Solutions

Solution 1

class Solution:
  def makeLargestSpecial(self, s: str) -> str:
    if s == '':
      return ''
    ans = []
    cnt = 0
    i = j = 0
    while i < len(s):
      cnt += 1 if s[i] == '1' else -1
      if cnt == 0:
        ans.append('1' + self.makeLargestSpecial(s[j + 1 : i]) + '0')
        j = i + 1
      i += 1
    ans.sort(reverse=True)
    return ''.join(ans)
class Solution {
  public String makeLargestSpecial(String s) {
    if ("".equals(s)) {
      return "";
    }
    List<String> ans = new ArrayList<>();
    int cnt = 0;
    for (int i = 0, j = 0; i < s.length(); ++i) {
      cnt += s.charAt(i) == '1' ? 1 : -1;
      if (cnt == 0) {
        String t = "1" + makeLargestSpecial(s.substring(j + 1, i)) + "0";
        ans.add(t);
        j = i + 1;
      }
    }
    ans.sort(Comparator.reverseOrder());
    return String.join("", ans);
  }
}
class Solution {
public:
  string makeLargestSpecial(string s) {
    if (s == "") return s;
    vector<string> ans;
    int cnt = 0;
    for (int i = 0, j = 0; i < s.size(); ++i) {
      cnt += s[i] == '1' ? 1 : -1;
      if (cnt == 0) {
        ans.push_back("1" + makeLargestSpecial(s.substr(j + 1, i - j - 1)) + "0");
        j = i + 1;
      }
    }
    sort(ans.begin(), ans.end(), greater<string>{});
    return accumulate(ans.begin(), ans.end(), ""s);
  }
};
func makeLargestSpecial(s string) string {
  if s == "" {
    return ""
  }
  ans := sort.StringSlice{}
  cnt := 0
  for i, j := 0, 0; i < len(s); i++ {
    if s[i] == '1' {
      cnt++
    } else {
      cnt--
    }
    if cnt == 0 {
      ans = append(ans, "1"+makeLargestSpecial(s[j+1:i])+"0")
      j = i + 1
    }
  }
  sort.Sort(sort.Reverse(ans))
  return strings.Join(ans, "")
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文