Red-Green-Code

Deliberate practice techniques for software developers

  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter

LeetCode 394: Decode String

By Duncan Smith Feb 3 0

LeetCode 2021

Problem

LeetCode 394: Decode String (Medium)

Problem Statement: Decode a string that has been encoded in a specified format.

In the encoded string, k[str] means decode the string str and repeat the result k times. The input encoding is always valid: k is always a positive integer, there is no extra whitespace, the square brackets match, etc. str may contain more letters, integers and brackets, following the same format rules. However, the decoded string will consist only of lowercase letters (no digits, brackets, or other characters).

Solution

The solution described here is based on this solution by bluedawnstar.

We will implement a recursive function, DecodeString. The solution strategy is to process the input string character by character in one pass, taking different actions based on whether the current character is a digit, a letter, or a closing bracket. Here are the three cases:

Digit

If the character is a digit, we know that this part of the string is an integer followed by an opening bracket, a substring, and a closing bracket. We can process this section using three steps:

  • Read all the digits before the opening bracket and interpret them as an integer n. This is the number of times to repeat the string inside the brackets.

  • Recursively call DecodeString on the string inside the brackets. That string may contain more digits, letters, and brackets, so there may be further recursive calls.

  • Append n copies of the result to the output string.

Letter

Append the letter to the output string.

Closing bracket

A closing bracket means a substring is complete, so return the result.

Pseudocode

for each character c in the input string
    if c is a digit
        convert consecutive digits to an integer n
        recursively call DecodeString
        append n copies of the result to the output string
    else if c is a letter, append it to the output string
    else if c is ']', return the current output string

One implementation detail: It simplifies the recursive function if we store the current input string position as a class-level variable. Since we’re always moving forward in the input string, this means a recursive call can just continue from the current position and doesn’t have to return how many input characters it processed. We can just initialize the position to 0 when the program starts, and increment it whenever we process a character. It doesn’t matter whether we’re in the first call to DecodeString or ten recursive calls deep.

Code

Decode String on GitHub

LeetCode Model Solutions

I’m posting some LeetCode model solutions this year. For more about this, see A Project for 2021.

Categories: LeetCode

Prev
Next

Stay in the Know

I'm trying out the latest learning techniques on software development concepts, and writing about what works best. Sound interesting? Subscribe to my free newsletter to keep up to date. Learn More
Unsubscribing is easy, and I'll keep your email address private.

Getting Started

Are you new here? Check out my review posts for a tour of the archives:

  • 2023 in Review: 50 LeetCode Tips
  • 2022 in Review: Content Bots
  • 2021 in Review: Thoughts on Solving Programming Puzzles
  • Lessons from the 2020 LeetCode Monthly Challenges
  • 2019 in Review
  • Competitive Programming Frequently Asked Questions: 2018 In Review
  • What I Learned Working On Time Tortoise in 2017
  • 2016 in Review
  • 2015 in Review
  • 2015 Summer Review

Archives

Recent Posts

  • Do Coding Bots Mean the End of Coding Interviews? December 31, 2024
  • Another Project for 2024 May 8, 2024
  • Dynamic Programming Wrap-Up May 1, 2024
  • LeetCode 91: Decode Ways April 24, 2024
  • LeetCode 70: Climbing Stairs April 17, 2024
  • LeetCode 221: Maximal Square April 10, 2024
  • Using Dynamic Programming for Maximum Product Subarray April 3, 2024
  • LeetCode 62: Unique Paths March 27, 2024
  • LeetCode 416: Partition Equal Subset Sum March 20, 2024
  • LeetCode 1143: Longest Common Subsequence March 13, 2024
Red-Green-Code
  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter
Copyright © 2025 Duncan Smith