Advertisement

Check If Word Is Valid After Substitutions - LeetCode 1003 Solution

Check If Word Is Valid After Substitutions - Complete Solution Guide

Check If Word Is Valid After Substitutions is LeetCode problem 1003, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 t left + "abc" + t right , where t == t left + t right . Note that t left and t right may be empty . Return true if s is a valid string, otherwise, return false . Example 1: Input: s = "aabcbc" Output: true Explanation: "" -> " abc

Detailed Explanation

The problem asks us to determine if a given string `s` is valid. A string is considered valid if it can be constructed by repeatedly inserting the string "abc" into any position of an initially empty string. Essentially, we need to check if we can reduce the string `s` back to an empty string by repeatedly removing "abc" from it.

Solution Approach

The solution uses a stack to simulate the process of removing "abc" from the string. The algorithm iterates through the input string character by character. If the current character is 'a' or 'b', it's pushed onto the stack. If the current character is 'c', the algorithm checks if the top two elements of the stack are 'b' and 'a' respectively. If they are, it pops both 'b' and 'a' from the stack, effectively removing an "abc" sequence. If the top elements are not 'b' and 'a', or if the stack doesn't contain enough elements, the string is invalid. After processing all characters, the algorithm checks if the stack is empty. An empty stack means the entire string has been reduced to an empty string, indicating the string is valid.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack (or array used as a stack).
  2. Step 2: Iterate through the input string `s` character by character.
  3. Step 3: If the current character is 'a' or 'b', push it onto the stack.
  4. Step 4: If the current character is 'c':
  5. Step 4.1: Check if the stack has at least two elements.
  6. Step 4.2: If not, the string is invalid, return false.
  7. Step 4.3: Pop the top element from the stack. If it's not 'b', the string is invalid, return false.
  8. Step 4.4: Pop the next element from the stack. If it's not 'a', the string is invalid, return false.
  9. Step 5: After processing all characters, check if the stack is empty.
  10. Step 6: If the stack is empty, the string is valid, return true. Otherwise, the string is invalid, return false.

Key Insights

  • Insight 1: The problem can be efficiently solved using a stack. When encountering an 'a' or 'b', push it onto the stack. When encountering a 'c', check if the top two elements of the stack are 'b' and 'a' respectively. If so, pop them off the stack; otherwise, the string is invalid.
  • Insight 2: The order of 'a', 'b', and 'c' matters. 'a' must come before 'b' which must come before 'c'. This suggests a last-in, first-out (LIFO) data structure like a stack is appropriate.
  • Insight 3: If, at the end of processing the string, the stack is empty, it means the entire string was successfully reduced to an empty string via "abc" removals, making it a valid string. If the stack is not empty, it means some characters could not be matched, and the string is invalid.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Stack.

Companies

Asked at: Nutanix.