leet-code/src/main/java/leetcode/editor/cn/CheckIfWordIsValidAfterSubstitutions.java
2021-04-29 23:21:52 +08:00

77 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个字符串 s ,请你判断它是否 有效 。
// 字符串 s 有效 需要满足:假设开始有一个空字符串 t = "" ,你可以执行 任意次 下述操作将 t 转换为 s
//
//
// 将字符串 "abc" 插入到 t 中的任意位置。形式上t 变为 tleft + "abc" + tright其中 t == tleft + trigh
//t 。注意tleft 和 tright 可能为 空 。
//
//
// 如果字符串 s 有效,则返回 true否则返回 false。
//
//
//
// 示例 1
//
//
//输入s = "aabcbc"
//输出true
//解释:
//"" -> "abc" -> "aabcbc"
//因此,"aabcbc" 有效。
//
// 示例 2
//
//
//输入s = "abcabcababcc"
//输出true
//解释:
//"" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
//因此,"abcabcababcc" 有效。
//
// 示例 3
//
//
//输入s = "abccba"
//输出false
//解释:执行操作无法得到 "abccba" 。
//
// 示例 4
//
//
//输入s = "cababc"
//输出false
//解释:执行操作无法得到 "cababc" 。
//
//
//
// 提示:
//
//
// 1 <= s.length <= 2 * 104
// s 由字母 'a'、'b' 和 'c' 组成
//
// Related Topics 栈 字符串
// 👍 46 👎 0
package leetcode.editor.cn;
//1003:检查替换后的词是否有效
public class CheckIfWordIsValidAfterSubstitutions {
public static void main(String[] args) {
//测试代码
Solution solution = new CheckIfWordIsValidAfterSubstitutions().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isValid(String s) {
while (s.contains("abc")) {
s = s.replace("abc", "");
}
return "".equals(s);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}