Mid/SeniorStackString

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 in t. More formally, t becomes tleft + "abc" + tright, where t == tleft + tright. Note that tleft and tright may be empty.

Return true if s is a valid string, otherwise, return false.

Example 1
Inputs = "aabcbc"
Outputtrue
Starting from the empty string, insertions can produce "abc" and then "aabcbc", so "aabcbc" is valid.
Example 2
Inputs = "abcabcababcc"
Outputtrue
Starting 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'

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate