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: false
Note:
1 <= S.length <= 20000S[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 ...
随机推荐
- DCloud-流应用:杂项
ylbtech-DCloud-流应用:杂项 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回 ...
- python的ftp上传和下载
# -*- coding: utf- -*- import os import ftplib USER_NAME = "" PASSWORD = "" SERV ...
- [Apache]如何查看apache服务器的error log(错误日志)
在进行网页和服务器的测试时, 有时会提醒 500 Internal Server Error: The server encountered an internal error or misconfi ...
- vue axios 应用
vue安装axios cnpm install axios 安装成功后/项目/node_modules/目录下有axios文件夹 在package.json文件中devDependencies字段中添 ...
- VxVM如何扩展和收缩卷及文件系统
1. 同时扩展卷和文件系统 先用vxassist命令检查DG可用空间 [root@rhelnode1 ~]# vxassist -g testdg maxsize Maximum volume siz ...
- xcode编译静态库选择cpu架构
此前编译了一个静态库,默认支持了armv7,armv7s,arm64 编译的话肯定是上面三个静态库分别编译出来,然后在把这三个合并成一个单独的库. 如果单个库的大小是10M,那编译总的库大概就30M了 ...
- asp.net的验证码插件及方法、ashx验证码一般处理程序
需要引入一个ashx的一般处理程序! 把这个程序在前台当作一个图片使用就可以! 前台代码: <td> <img title="看不清?" style=" ...
- sed对指定行添加或删除注释
如下文本 zimu.txt aaaaa #bbbbbb cccccc dddddd 以下命令如果需要在文本中更改 需要加 -i 或者 -ri参数 用sed在aaa前加#注释 sed 's/^ ...
- Android指针管理:RefBase,SP,WP (二)
(1)在Android中,RefBase结合了sp和wp,实现了一套通过引用计数的方法来控制对象声明周期的方法. RefBase的定义在/frameworks/base/include/utils/R ...
- StackMapTable format error
环境:Oracle Java 7 , Mac OSX 报错如上图所示,主要是 Caused by: java.lang.ClassFormatError: StackMapTable format e ...