Check If Word Is Valid After Substitutions
Medium · String, Stack
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:
Return true if s is a valid string, otherwise, return false.
Example 1
Input: s = "aabcbc" Output: true
"" -> "abc" -> "aabcbc" Thus, "aabcbc" is valid.
Example 2
Input: s = "abcabcababcc" Output: true
"" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc" Thus, "abcabcababcc" is valid.
Example 3
Input: s = "abccba" Output: false
It is impossible to get "abccba" using the operation.
Constraints
- 1 <= s.length <= 2 * 104
- s consists of letters 'a', 'b', and 'c'
Starter code (python)
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
Related problems
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
- Roman to Integer (Easy)
Previous: Find Common Characters · Next: Clumsy Factorial · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.