Check If Word Is Valid After Substitutions
Given a string s, determine if it is valid.
A string s is valid if, starting with an empty string t = "", you can transform t into s after performing the following operation any number of times:
- Insert string
"abc"into any position int. More formally,tbecomestleft + "abc" + tright, wheret == tleft + tright. Note thattleftandtrightmay be empty.
Return true if s is a valid string, otherwise, return false.
Example 1
Input
s = "aabcbc"Output
trueStarting from the empty string, insertions can produce "abc" and then "aabcbc", so "aabcbc" is valid.
Example 2
Input
s = "abcabcababcc"Output
trueStarting from the empty string, insertions can produce "abc", then "abcabc", then "abcabcabc", and then "abcabcababcc", so it is valid.
Constraints
- 1 <= s.length <= 2 * 10^4
- s consists of letters 'a', 'b', and 'c'