题目如下:

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. 1 <= S.length <= 20000
  2. 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的更多相关文章

  1. 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)

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

  2. 1003. Check If Word Is Valid After Substitutions Medium检查替换后的词是否有效

    网址:https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/ 参考:https://leetcode.com ...

  3. 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 ...

  4. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  5. 【LeetCode】748. Shortest Completing Word 解题报告(Python)

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

  6. 【LeetCode】819. Most Common Word 解题报告(Python)

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

  7. 【leetcode】Length of Last Word

    题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  8. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

  9. 【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 ...

随机推荐

  1. vue 生命周期函数详解

    beforeCreate( 创建前 ) 在实例初始化之后,数据观测和事件配置之前被调用,此时组件的选项对象还未创建,el 和 data 并未初始化,因此无法访问methods, data, compu ...

  2. 【Java】定义Logger为什么要用static和final?

    private static final Logger logger= LoggerFactory.getLogger(ShiroConfig.class); (1)出于资源利用的考虑,Logger的 ...

  3. (转载)虚拟机出现无法连接虚拟设备sata0:0,因为主机上没有相应设备

    虚拟主机需要镜像文件, 如果是拷贝的虚拟机,还需要桥接联网的话,更改mac地址,

  4. (转)PAL制式和NTSC制式的区别

    转:https://www.cnblogs.com/nx520zj/articles/6061777.html 常见的电视信号制式是PAL和NTSC,另外还有SECAM等. NTSC即正交平衡调幅制. ...

  5. VS2010提示error TRK0002: Failed to execute command

    转自VC错误:http://www.vcerror.com/?p=277 问题描述: windows8自动更新Microsoft .NET Framework 3.5和4.5.1安全更新程序,今天用V ...

  6. PAT甲级——A1155 HeapPaths【30】

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  7. C# 几进制 转换到几进制

    public string ConvertString(string value, int fromBase, int toBase) { int intValue = Convert.ToInt32 ...

  8. JavaScript 获取function的参数

    function getArgs(func) { // 先用正则匹配,取得符合参数模式的字符串. // 第一个分组是这个: ([^)]*) 非右括号的任意字符 var args = func.toSt ...

  9. 路由参数 query和params

    1. path:'www.baidu.com' query  { id:122 } 对应地址:http:'www.baidu.coom?id=122'   类似get方式 2.name:'baidu' ...

  10. redis集群搭建(简单简单)一台机器多redis

      redis集群搭建 在开始redis集群搭建之前,我们先简单回顾一下redis单机版的搭建过程 下载redis压缩包,然后解压压缩文件: 进入到解压缩后的redis文件目录(此时可以看到Makef ...