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: false
Note:
1 <= S.length <= 20000
S[i]
is'a'
,'b'
, or'c.
Approach #1: Stack. [Java]
class Solution {
public boolean isValid(String S) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < S.length(); ++i) {
if (S.charAt(i) == 'c') {
if (stack.empty() || stack.pop() != 'b') return false;
if (stack.empty() || stack.pop() != 'a') return false;
} else stack.push(S.charAt(i));
}
return stack.empty();
}
}
Analysis:
Keep a stack, whenever meet character of 'c', pop 'b' and 'a' at the end of the stack. Otherwise, return false;
Reference:
1003. Check If Word Is Valid After Substitutions的更多相关文章
- 【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 split V ...
- 【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 ...
- [Swift]LeetCode1003. 检查替换后的词是否有效 | 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 ...
- qvalue: Check that you have valid p-values or use a different range of lambda
ERROR: The estimated pi0 <= 0. Check that you have valid p-values or use a different range of lam ...
- Word: How to Temporarily Disable Spell Check in Word
link: http://johnlamansky.com/tech/disable-word-spell-check/ 引用: Word 2010 Click the “File” button C ...
- Weekly Contest 126
前两周一直是一道,都不好意思写了.这周终于题目简单了,刷了三道. 1002. Find Common Characters Given an array A of strings made only ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- CentOS6.5下安装mongodb
MongoDB是目前最常用的NoSQL-非关系型数据库. 本文将介绍在CentOS下如何通过yum安装MongoDB. 1.首先在CentOS6.5下,编辑Mongo的yum源: 在/etc/yum. ...
- Java基础--CountDownLatch
CountDownLatch是线程同步辅助类,它允许一个或多个线程wait直到countdown被调用使count为0. CountDownLatch是在java1.5被引入,存在于java.util ...
- a(+;-;*;/)b-----demo----bai
页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&qu ...
- python 面向对象之反射及内置方法
面向对象之反射及内置方法 一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静 ...
- Solaris10安装配置LDAP(iPlanet Directory Server )
Solaris10安装光盘自带了iPlanet Directory Server安装包,系统管理员可以利用iPlanet Directory Server在Solaris系统创建一个LDAP Serv ...
- Hadoop运行程序不报错只有warn也没反应也不输出结果的解决办法
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...
- springmvc 注解式开发 处理器方法的返回值
1.返回void -Ajax请求 后台: 前台: 返回object中的数值型: 返回object中的字符串型: 返回object中的自定义类型对象: 返回object中的list: 返回object中 ...
- re.I re.L re.M re.S re.U re.X
- linux ORACLE备份还原(EXP\IMP)
一.Oracle导入导出 1.Oracle的备份是Oracle操作中常见的工作,常见的备份方案有:逻辑备份(IMP&EXP命令进行备份).物理文件备份(脱机及联机备份).利用RMAN(Reco ...
- sharepoint文档库中日期显示详细日期,不显示几天前
文档库---库设置----栏