作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/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.

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.

题目大意

一个特殊的二进制字符串满足以下两个属性:

  1. 字符串中0的个数等于1的个数
  2. 这个字符串中任何前缀中1的个数不少于0的个数

给了一个满足要求的特殊字符串,每次移动可以选择两个两个连续的非空的特殊二进制子串进行交换。求在一系列移动之后,能得到的字母顺序的字符串最大结果。

解题方法

这道题的原型是括号匹配问题。求括号匹配的经典的方法是使用cnt记数的方法,如果是左括号,那么cnt+1,如果是右括号,那么cnt-1,如果cnt等于0了,说明这已经是个匹配了的括号了。注意,一个括号匹配问题中可能存在多个匹配的括号,这个题也是,比如1010就是满足题意的二进制字符串,相等于两个满足题意的二进制串组成。

如果要想在一系列移动之后,得到最大的字符串,那么可以看出要求1尽量在前面,同时0尽量在后面。我们使用list保存那些符合要求的字符串,最后排序即可。一个符合要求的字符串其开头必须是1,末尾必须是0。同时我们意识到,符合要求的字符串的内部也要进行排序。比如子串1010,要给他排序成1100这个样子,注意中间的字符串01并不符合题目要求,给他重新排成10样式,使用递归结构。使用i保存上一次判断完成的字符串的结尾,用j遍历字符串。每次判断结束一个符合要求的子串之后,要令 i = j + 1。

最坏情况下的时间复杂度是O(N!),最优情况下的时间复杂度是O(1),空间复杂度是O(N)。

class Solution(object):
def makeLargestSpecial(self, S):
"""
:type S: str
:rtype: str
"""
cnt = 0
res = list()
i= 0
for j, v in enumerate(S):
cnt += 1 if v == "1" else -1
if cnt == 0:
res.append("1" + self.makeLargestSpecial(S[i + 1:j]) + "0")
i = j + 1
return "".join(sorted(res, reverse=True))

参考资料:

http://www.cnblogs.com/grandyang/p/8606024.html
https://blog.csdn.net/u014688145/article/details/78996824
https://blog.csdn.net/BambooYH/article/details/80686074
http://www.cnblogs.com/zzuli2sjy/p/8260854.html

日期

2018 年 10 月 2 日 —— 小蓝单车莫名其妙收了我1块钱,明明每个月免费骑10次的啊!

【LeetCode】761. Special Binary String 解题报告(Python)的更多相关文章

  1. leetcode 761. Special Binary String

    761. Special Binary String 题意: 一个符合以下两个要求的二进制串: \(1.串中包含的1和0的个数是相等的.\) \(2.二进制串的所有前缀中1的个数不少于0的个数\) 被 ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 761. Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  4. 【LeetCode】401. Binary Watch 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 java解法 Python解法 日期 [LeetCo ...

  5. 【LeetCode】87. Scramble String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...

  6. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  7. 【LeetCode】796. Rotate String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】767. Reorganize String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...

  9. 【LeetCode】344. Reverse String 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...

随机推荐

  1. CMSIS-RTOS 信号量Semaphores

    信号量Semaphores 和信号类似,信号量也是一种同步多个线程的方式,简单来讲,信号量就是装有一些令牌的容器.当一个线程在执行过程中,就可能遇到一个系统调用来获取信号量令牌,如果这个信号量包含多个 ...

  2. 修改unittest源码之tearDown

    需求 最近在写selenium自动化平台,想把每条用例后面都带上截图,最开始是每条用例加上封装好的截图函数,但是发现太麻烦,就决定加在tearDown函数里面,每条用例结束后执行截图操作. 那么问题来 ...

  3. 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用

    7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...

  4. Shell $()、${}、$[]、$(())

    目录 Shell中的 $().${}.$[].$(()) $().${} 替换 ${} 变量内容的替换.删除.取代 数组 $[].$(()) 运算符 Shell中的 $().${}.$[].$(()) ...

  5. 学习java 7.8

    学习内容: 被static修饰的不需要创建对象,直接用类名引用即可 内部类访问特点:内部类可以直接访问外部类的成员,包括私有 外部类访问内部类的成员,必须创建对象 成员内部类,内部类为私有,Outer ...

  6. [C++] vptr, where are you?

    Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...

  7. day08 文件属性

    day08 系统目录 今日内容 一.重要目录 1./usr 2./var 3./proc 二.文件的属性 1.文件属性的介绍 2.文件属性的详述 3.企业案例 /usr 安装第三方软件的目录: 1./ ...

  8. Flink基础

      一.抽象层次 Flink提供不同级别的抽象来开发流/批处理应用程序. 最低级抽象只提供有状态流.它 通过Process Function嵌入到DataStream API中.它允许用户自由处理来自 ...

  9. oracle to_char处理日期

    select to_char(sysdate,'d') from dual;--本周第几天 select to_char(sysdate,'dd') from dual;--本月第几天 select ...

  10. RHEL 6.5安装系统

    转自如下链接: http://www.jb51.net/os/128752.html