#1003

Check If Word Is Valid After Substitutions

specialist · 735 · lc medium +31 · verified · 61.1% accepted · 1,082 likes · top 61%

Description

Given a string s, determine whether it is valid.

A string is valid if it can be produced from an empty string t = "" by any number of insertions of "abc" at any position:

- t becomes tleft + "abc" + tright, where t == tleft + tright.

Return true if s is valid, false otherwise.

Example 1:

Input: s = "aabcbc"
Output: true
Explanation:
"" -> "abc" -> "aabcbc"
Thus, "aabcbc" is valid.

Example 2:

Input: s = "abcabcababcc"
Output: true
Explanation:
"" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
Thus, "abcabcababcc" is valid.

Example 3:

Input: s = "abccba"
Output: false
Explanation: It is impossible to get "abccba" using the operation.

Code

1
2
3