【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 splitV
into two piecesX
andY
such thatX + Y
(X
concatenated withY
) is equal toV
. (X
orY
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 stringS
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: falseExample 4:
Input: "cababc"
Output: falseNote:
1 <= S.length <= 20000
S[i]
is'a'
,'b'
, or'c'
解题思路:这个题目有点用巧的意思了,可以逆向思维。每次从input中删除掉一个"abc",如果最后input能变成空就是True,否则就是False。
代码如下:
class Solution(object):
def isValid(self, S):
"""
:type S: str
:rtype: bool
"""
while len(S) > 0:
inx = S.find('abc')
if inx == -1:
return False
S = S[:inx] + S[inx+3:]
return True
【leetcode】1003. Check If Word Is Valid After Substitutions的更多相关文章
- 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...
- 1003. Check If Word Is Valid After Substitutions Medium检查替换后的词是否有效
网址:https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/ 参考:https://leetcode.com ...
- 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 ...
- 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...
- 【LeetCode】748. Shortest Completing Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- 【leetcode】Length of Last Word
题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
- 【leetcode】1232. Check If It Is a Straight Line
题目如下: You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coord ...
随机推荐
- Linux中Hard link和Symbol link的区别
Hard link Hard link不能指向不在同一磁盘的文件 Hard link不能指向目录 Hard link与源文件几乎没有区别.只能通过ls -li看出link关系.另外,删除源文件后,Ha ...
- SQL Server 2008 R2 数据库备份文件.bak如何挂载到【阿里云·独立虚拟主机数据库】上
1.前言 8月份刚刚开始,公司[工作流]挂了,实体服务器会自动重启,数据库死活起不来,这可是计算工资提成的事儿,不能马虎! 在实体服务器中,找到了OA页面源码与数据库的.MDF 与 .LDF 等文件. ...
- Delphi 运行后错误提示“无效的授权说明”
Delphi 运行后错误提示“无效的授权说明” 一般情况是:数据库的连接出现了问题. 解决方法:检查加载数据库是否正常,能否正常连接.
- boost heap
1. using boost::heap::priority_queue #include <boost/heap/priority_queue.hpp> #include <ios ...
- Win7隐藏登录界面中的用户(不建议HOME版使用)
一天一點 能登多高,靠的不是双脚!能看多远,靠的不是双眼!人生路,贵在坚持! Win7隐藏登录界面中的用户(不建议HOME版使用) Win7中如何隐藏不想出现在登录界面中的用户 在Windows系统管 ...
- LocalActivityManager如何在一个Activity的一部分中显示其他Activity
首先要使用该方法,页面必须继承ActivityGroup. 总的来说,实现"如何在一个Activity的一部分中显示其他Activity"除了LocalActivityManage ...
- 听说你懂个J?——前端发展闲聊
刚好周末和朋友聊起"前端从受鄙视到变得重要"这个话题,感慨前端这四年来的发展,遂有本文. 1. 前情提要 毋庸讳言,在我刚工作的时候,前端是还是一个不受重视的岗位.切图狗,写网页的 ...
- php面试专题---3、运算符考察点
php面试专题---3.运算符考察点 一.总结 一句话总结: 逻辑运算符注意短路效果,优先级问题直接用括号,还要注意 ||和&&与or和and的优先级不同 1.foo()和@foo() ...
- JS:收集的一些Array及String原型对象的扩展实现代码
扩展Array的原型对象的方法 // 删除数组中数据 Array.prototype.del = function(n) { if (n<0) return this; return this ...
- jmeter添加自定义扩展函数之MD5加密
1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...