【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 ...
随机推荐
- mui滚动区域的实现
mui框架实现页面中间区域滚动,头部和底部固定不动,要滚动的区域一定要有mui-scroll-wrapper 和 mui-scroll 包裹 <div class="mui-conte ...
- .Net Core 使用Redis进行数据缓存
1.运行环境 开发工具:Visual Studio 2017 JDK版本:.NET Core 2.0 项目管理工具:nuget 2.GITHUB地址 https://github.com/nbfujx ...
- windows下用VMware虚拟机下安装Linux CentOS6.9图文教程
首先,请在Windows7下安装VMware虚拟机,这个比较简单,直接从官网下载安装即可,这里不再叙述. 接着,从官网直接下载CentOS6.9的iso镜像文件,地址:https://www.cent ...
- JS谷歌浏览器断点调试
1.找到对应的文件 按F12打开网页调试工具,默认打开的是Elements,显示的是网页标签元素.选择Source,在左侧找到对应的js代码文件(这里是在page标签上找到的) 1.1.如何找到web ...
- 【UNR #2】黎明前的巧克力 解题报告
[UNR #2]黎明前的巧克力 首先可以发现,等价于求 xor 和为 \(0\) 的集合个数,每个集合的划分方案数为 \(2^{|S|}\) ,其中 \(|S|\) 为集合的大小 然后可以得到一个朴素 ...
- 图与例解读Async/Await
JavaScript ES7的async/await语法让异步promise操作起来更方便.如果你需要从多个数据库或者接口按顺序异步获取数据,你可能最终写出一坨纠缠不清的promise与回调.然而使用 ...
- 用DELPHI中实现RAR文件解压到指定一目录
一个RAR压缩文件,用DELPHI编的程序打开它并解压到某一目录,怎么实现的?自己搞定了例子:winrar.exe e -y C:\WINDOWS\Desktop\ghost.rar d:\ 但新的问 ...
- python 简单抓取网页并写入excel实例
# -*- coding: UTF-8 -*- import requests from bs4 import BeautifulSoup import xlwt import time #获取第一页 ...
- 正确设置nginx/php-fpm/apache权限 提高网站安全性 防止被挂木马
核心总结:php-fpm/apache 进程所使用的用户,不能是网站文件所有者. 凡是违背这个原则,则不符合最小权限原则. 根据生产环境不断反馈,发现不断有 php网站被挂木马,绝大部分原因是因为权限 ...
- python中socket理论
Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单 ...