【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 splitVinto two piecesXandYsuch thatX + Y(Xconcatenated withY) is equal toV. (XorYmay be empty.) Then,X + "abc" + Yis 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
trueif and only if the given stringSis 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 <= 20000S[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 ...
随机推荐
- 为什么js的"关联数组"不能转成json字符串而对象可以?
定义这么一个js的“关联数组”: var arr = new Array(); arr[; arr[; alert(JSON.stringify(arr)); 得到的结果如图: [] 一句话,你的 a ...
- python如何在shell命令行执行创建用户命令
- C# 私有字段前缀 _ 的设置(VS2019, .editorconfig)
常量和静态只读字段大写 私有字段前缀 _ #### Naming styles #### # Naming rules dotnet_naming_rule.const_should_be_all_u ...
- Mac查询电脑mac地址
方法一: 按住键盘上的“Windows+R”,然后在弹出的运行框中输入“CMD”或依次点击 开始>所有程序>附件>命令提示符 在弹出的命令提示符窗口中输入“ipconfig /all ...
- ElasticSearch 简介概念及核心
1.ES是什么 ES是面向文档的Nosql,这意味着它可以存储整个对象或文档.然而它不仅仅是存储,还会索引(index)每个文档的内容使之可以被搜索.在es中,你可以对文档(而非成行成列的数据)进行索 ...
- LintCode之各位相加
题目描述: 我的代码 public class Solution { /* * @param num: a non-negative integer * @return: one digit */ p ...
- EasyUI 中的双击某行 并赋值给input事件
项目是由mvc+easyUI开发,双击事件在下边.有注释写着呢 function DataList(supCode) { myDatagrid2.datagridId = "GridView ...
- JavaScript-打开新窗口(window.open)和 关闭窗口(window.close)
JavaScript-打开新窗口 open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL:可选 ...
- js 监听input 实现数据绑定
<!DOCTYPE html> <html> <head> <script> function checkField(val) { //alert(&q ...
- Skimap_ros 利用RGBD创建Octomap(一)
1. 奥比中光astra RGBD相机安装 1.1 安装依赖 $ sudo apt-get install build-essential freeglut3 freeglut3-dev 1.2 检查 ...