【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/
题目描述
We are given that the string "abc"
is valid.
From any valid string V
, we may split V
into two pieces X
and Y
such that X + Y
(X
concatenated with Y
) is equal to V
. (X
or Y
may be empty.) Then, X + "abc" + Y
is also valid.
If for example S = "abc"
, then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc"
. Examples of invalid strings are: "abccba", "ab", "cababc", "bac"
.
Return true
if and only if the given string S
is valid.
Example 1:
Input: "aabcbc"
Output: true
Explanation:
We start with the valid string "abc".
Then we can insert another "abc" between "a" and "bc", resulting in "a" + "abc" + "bc" which is "aabcbc".
Example 2:
Input: "abcabcababcc"
Output: true
Explanation:
"abcabcabc" is valid after consecutive insertings of "abc".
Then we can insert "abc" before the last letter, resulting in "abcabcab" + "abc" + "c" which is "abcabcababcc".
Example 3:
Input: "abccba"
Output: false
Example 4:
Input: "cababc"
Output: false
Note:
1 <= S.length <= 20000
S[i] is 'a', 'b', or 'c'
题目大意
初始只给了一个"abc"
字符串,然后可以在该字符串的任意位置插入一个新的"abc"
字符串,然后继续该操作……这些步骤得到的字符串均为有效字符串。如果经过上面的操作无论如何都不能得到的字符串就是无效字符串。
问一个字符串是不是有效字符串。
解题方法
循环
好久没有做过这么简单的题目了……
如果理解了题意应该明白,每次都去插入一个"abc"
字符串,那么到最后一步一定会有"abc"
。所以我们如果倒着来,每次把"abc"
进行替换成""
,那么一定能回到最初的状态,也就是空字符串。
这个方法唯一不太好的是时间复杂度,判断"abc"是否在S中,需要O(N)的时间复杂度,替换这步的操作应该也是O(N),所以总的时间复杂度是O(N^2)。题目给出的S的长度是20000,为什么没有超时呢?我觉得主要是替换的时候会把大量的"abc"同时替换掉了,这样每次操作就把字符串大幅变短了。
python代码如下:
class Solution(object):
def isValid(self, S):
"""
:type S: str
:rtype: bool
"""
while "abc" in S:
S = S.replace("abc", "")
return not S
日期
2019 年 3 月 3 日 —— 3月开始,春天到了
【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)的更多相关文章
- 【leetcode】1003. Check If Word Is Valid After Substitutions
题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...
- 1003. Check If Word Is Valid After Substitutions
We are given that the string "abc" is valid. From any valid string V, we may split V into ...
- 1003. Check If Word Is Valid After Substitutions Medium检查替换后的词是否有效
网址:https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/ 参考:https://leetcode.com ...
- [Swift]LeetCode1003. 检查替换后的词是否有效 | Check If Word Is Valid After Substitutions
We are given that the string "abc" is valid. From any valid string V, we may split V into ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- R语言实战(第二版)-part 1笔记
说明: 1.本笔记对<R语言实战>一书有选择性的进行记录,仅用于个人的查漏补缺 2.将完全掌握的以及无实战需求的知识点略去 3.代码直接在Rsudio中运行学习 R语言实战(第二版) pa ...
- 【Python小试】将核酸序列翻译成氨基酸序列
三联密码表 gencode = { 'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M', 'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT' ...
- day09 文件属性
day09 文件属性 昨日回顾 yum底层原理: 第一步:执行yum install nginx安装命令 第二步:yum去/etc/yum.repos.d这个目录中 第三步:根据/etc/yum/re ...
- Flume(一)【概述】
目录 一.Flume定义 二.Flume基础架构 1.Agent 2.Source 3.Sink 4.Channel 5.Event 一.Flume定义 Flume是Cloudera公司提供的一个 ...
- Hive(十二)【调优】
目录 1.Fetch抓取 2.本地模式 3.表的优化 3.1大小表join 3.2大表Join大表 3.3map join 3.4group By 3.5 count(distinct) 3.6笛卡尔 ...
- flink---实时项目--day01--1. openrestry的安装 2. 使用nginx+lua将日志数据写入指定文件中 3. 使用flume将本地磁盘中的日志数据采集到的kafka中去
1. openrestry的安装 OpenResty = Nginx + Lua,是⼀一个增强的Nginx,可以编写lua脚本实现⾮非常灵活的逻辑 (1)安装开发库依赖 yum install -y ...
- javaIO——输入输出流
字节流与字符流 File类不支持对文件内容进行相关的操作,所有若要处理文件的内容,则需要通过流操作模式来完成. 流的基本操作步骤: Step1:根据文件路径创建File类对象. Step2:根据字节流 ...
- RAC中常见的高级用法-过滤
filter 过滤信号,使用它可以获取满足条件的信号. - (void)filter { //只有当我们文本框内容长度大于5才想要获取文本框的内容 [[_passWord.rac_textS ...
- zabbix之故障自治愈和分层报警
在agent端修改配置文件 root@ubuntu:~# vim /etc/sudoers zabbix ALL=(ALL) NOPASSWD:ALL#:重启服务root@ubuntu:~# syst ...
- dbeaver可视化工具-连接clickhouse
下载地址 https://dbeaver.io/download/ 本次下载的是ZIP包,这种方式方便移动/备份软件 此软件可连接多种数据库,这里是连接clickhouse 新建连接 点击 上面的倒三 ...