Leetcode Problem 394. Decode String

394. Decode String

Leetcode Solutions

Using Stack

  1. Initialize an empty stack.
  2. Iterate over each character in the input string.
  3. If the character is a digit, parse the full number.
  4. If the character is '[', push the current number and decoded string onto their respective stacks and reset them.
  5. If the character is a letter, append it to the current decoded string.
  6. If the character is ']', pop the stack to get the last number and decoded string, then append the current decoded string repeated number times to the decoded string.
  7. After the iteration, return the decoded string.
UML Thumbnail

Using Recursion

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...