【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: false
Example 4:
- Input: "cababc"
- Output: false
Note:
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 ...
随机推荐
- JSP页面中<%!%>与<%%>与<%=%>详解
首先,我们要了解jsp运行原理.JSP的本质就是一个Servlet,JSP的运行之前会先被Tomcat服务器翻译为.java文件,然后在将.java文本编译 为.class文件,而我们在访问jsp时, ...
- CDH6.3 Centos7
按照官方文档安装即可 CentOS7 上搭建 CDH(6.3.0) 官方文档:https://docs.cloudera.com/documentation/enterprise/6/6.3/topi ...
- 【BZOJ2639】矩形计算(二维普通莫队)
题意:输入一个n*m的矩阵,矩阵的每一个元素都是一个整数,然后有q个询问,每次询问一个子矩阵的权值. 矩阵的权值是这样定义的,对于一个整数x,如果它在该矩阵中出现了p次,那么它给该矩阵的权值就贡献p^ ...
- S1 Python 基础
定义规范 声明变量 name = "Alex Li" 变量定义规则 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名['and' ...
- python3下tomorow模块报语法错误def async(n, base_type, timeout=None): ^ SyntaxError: invalid syntax
python3 pip 安装tomorrow模块,调用时候会报错:def async(n, base_type, timeout=None): ^ SyntaxError: invalid synta ...
- tomcat启动前端项目
前后端分离项目,前端使用vue,部署启动前端项目可以使用NodeJS,Nginx,Tomcat. *)使用Tomcat部署启动: 1.把vue项目build生成的dist包,放到Tomcat的weba ...
- 深入浅出HashMap
/** *@ author ViVi *@date 2014-6-11 */ Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.希望通过仪器讨论.共同提高~ 1 ...
- Python 多进程异常处理
前言 最近项目用到了Python作为网站的前端,使用的框架是基于线程池的Cherrypy,但是前端依然有一些比较‘重’的模块.由于python的多线程无法很好的利用多核的性质,所以觉得把这些比较‘重’ ...
- [python面试题] 什么是单例,单例有什么用,业务场景是什么
单例概念: 单例是一个特殊的类,这个类只能创建一次实例,例子如下: 1.a = Std(name='leo'), b = Std(name='jack'),两者的指向都是name=‘leo’的对象: ...
- 15. Jmeter-配置元件二
jmeter-配置元件介绍与使用 JDBC Connection Configuration Java请求默认值 密钥库配置 LDAP Extended Request Defaults LDAP请求 ...