Check If Word Is Valid After Substitutions - Problem
String Validation Through Substitution Rules

Imagine you have a special string construction game where you start with an empty string and can only perform one operation: insert the substring "abc" at any position. Your task is to determine if a given string s could have been created using only this operation.

šŸŽÆ The Challenge: Given a string s, return true if it's possible to construct s by starting with an empty string and repeatedly inserting "abc" at any position, or false otherwise.

Example Construction:
• Start: "" (empty)
• Insert "abc": "abc"
• Insert "abc" at position 1: "aabcbc" = "aabcbc"
• Insert "abc" at end: "aabcbcabc" = "aabcbcabc"

This problem tests your understanding of stack-based parsing and pattern matching algorithms.

Input & Output

example_1.py — Basic Valid String
$ Input: s = "aabcbc"
› Output: true
šŸ’” Note: We can construct this string: '' → 'abc' → 'aabcbc' (insert 'abc' at position 1). The stack approach: push 'a', push 'a', push 'b', push 'c' (forms 'abc' with previous 'a','b', so pop them), push 'b', push 'c' (forms 'abc', so pop them). Stack becomes empty.
example_2.py — Simple ABC
$ Input: s = "abc"
› Output: true
šŸ’” Note: Direct single insertion: '' → 'abc'. Stack approach: push 'a', push 'b', push 'c' (forms complete 'abc', pop all). Stack becomes empty.
example_3.py — Invalid Pattern
$ Input: s = "abccba"
› Output: false
šŸ’” Note: After processing with stack: 'a','b','c' form 'abc' (popped), then 'c','b','a' remain. Since we can't form valid 'abc' patterns with remaining characters, the string is invalid.

Visualization

Tap to expand
Stack-Based ABC Pattern ValidationaPush 'a'bPush 'b'cPush 'c'ABC Pattern Detected!Remove Patternāœ“Stack EmptyKey InsightInstead of repeatedly searching and removing 'abc' substrings,use a stack to instantly detect and remove 'abc' patterns as they form.Time: O(n) | Space: O(n) | Single Pass SolutionabcValid ABC Block
Understanding the Visualization
1
Initialize
Start with empty stack and input string
2
Process Characters
Push each character onto the stack
3
Pattern Detection
When 'c' is pushed, check for 'abc' pattern
4
Pattern Removal
If 'abc' found, remove all three characters
5
Final Check
String is valid if stack is empty at the end
Key Takeaway
šŸŽÆ Key Insight: Use a stack to efficiently validate ABC patterns by detecting and removing complete 'abc' sequences as they form, achieving optimal O(n) performance.

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

In worst case, we might need to scan the string multiple times, each time removing one 'abc'

n
2n
⚠ Quadratic Growth
Space Complexity
O(n²)

Recursive calls and string copying can use quadratic space

n
2n
⚠ Quadratic Space

Constraints

  • 1 ≤ s.length ≤ 2 Ɨ 104
  • s consists only of letters 'a', 'b', and 'c'
  • Follow-up: Can you solve this in O(n) time with O(1) extra space?
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
42.3K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen