链接:https://leetcode-cn.com/problems/check-if-word-is-valid-after-substitutions/
把字符串中的abc一个一个地去掉,检查剩下的是否是空串即可。
java代码:
class Solution {
public boolean isValid(String S) {
while(S.contains("abc")) {
S = S.replaceAll("abc","");
}
return S.equals("");
}
}
python代码:
class Solution(object):
def isValid(self, S):
"""
:type S: str
:rtype: bool
"""
while S != '' and 'abc' in S:
S = S.replace('abc','')
return S == ''