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.  (Xor 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.

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:

https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/discuss/247626/JavaPythonC%2B%2B-Stack-Solution-O(N)

1003. Check If Word Is Valid After Substitutions的更多相关文章

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

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

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

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

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

  4. [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 ...

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

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

  7. Weekly Contest 126

    前两周一直是一道,都不好意思写了.这周终于题目简单了,刷了三道. 1002. Find Common Characters Given an array A of strings made only ...

  8. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. [转载]centos下yum安装samba及配置

    centos下yum安装samba及配置 在我们使用 Windows 作为客户机的时候,通常有文件.打印共享的需求.作为Windows 网络功能之一,通常可以在 Windows 客户机之间通过Wind ...

  2. 想开发VR游戏?你需要注意这些东西

    转自:http://www.gamelook.com.cn/2016/03/246620 开发VR游戏很难吗?有些人会说是,但在HTC虚拟现实新科技部门副总经理鲍永哲看来,VR游戏的门槛并不比一般的游 ...

  3. oracle sql 函数

    (7)查询日期之间的数据 例如查询student表中出生日期(birthday)在’2016-01-01’ 和’2017-01-01’之间的数据: select * from student wher ...

  4. Samba服务学习报错总结

    1 2 3 4 5 此文献来至百度文库 http://wenku.baidu.com/link?url=hkHembjXcjoYRU9ky34a46Lzv5SAEutwa0v1_F8INQsdg_KK ...

  5. matlab学习笔记(3)

    数据分析: 多项式: 多项式表示:p = [1 2 3 0]; //表示 1*x^3+2*x^2+3*x^1+0 ,系数从高次向低次项,0系数不能省略. roots函数:求解多项式的根.roots(p ...

  6. 在JAVA中,String,Stringbuffer,StringBuilder 的区别

    首先是,String,StringBuffer的区别 两者的主要却别有两方面,第一是线程安全方面,第二是效率方面 线程安全方面: String  不是线程安全的,这意味着在不同线程共享一个String ...

  7. Lucene源码解析--Analyzer之Tokenizer

    Analyzer包含两个核心组件,Tokenizer以及TokenFilter.两者的区别在于,前者在字符级别处理流,而后者则在词语级别处理流.Tokenizer是Analyzer的第一步,其构造函数 ...

  8. 用于.NET环境的时间测试(转)

    用于.NET环境的时间测试   在.NET环境中,衡量运行完整算法所花费的时间长度,需要考虑很多 需要考虑很多种情况 ,如:程序运行所处的线程以及无用单位收集(GC垃圾回收). 在程序执行过程中无用单 ...

  9. WCF客户端调用并行最大同时只支持两个请求

    做项目的时候发现 频繁调用WCF服务时 明明一次性发起了几十个请求 而在服务端记录的日志却显示出现了排队的迹象 并且都是最大并发数为2 在网上狂搜 大家给出来的解决方法都是增加web.config里面 ...

  10. Python_pip_02_利用pip安装模块(以安装pyperclip为例)

    >任务:利用pip安装pyperclip模块 >前提 你已经在你的电脑里面安装啦Python2.7的Windows版本,并且已经配置了环境变量 >实现步骤 >>打开你的P ...