Decode String - Complete Solution Guide
Decode String is LeetCode problem 394, 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 an encoded string, return its decoded string. The encoding rule is: k[encoded_string] , where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k . For exam
Detailed Explanation
The problem asks us to decode a string that follows a specific encoding rule: `k[encoded_string]`, where `encoded_string` is repeated `k` times. The input string will consist of lowercase English letters, digits, and square brackets. The integer `k` represents the number of times the `encoded_string` inside the brackets should be repeated. The goal is to return the fully decoded string.
Solution Approach
The provided solutions use a stack-based approach to handle the nested structure of the encoded string. The algorithm iterates through the string character by character. When a digit is encountered, it accumulates it to form the repetition count. When an opening bracket '[' is encountered, the current string and repetition count are pushed onto the stack. When a closing bracket ']' is encountered, the algorithm pops the previous string and repetition count from the stack, repeats the current string by the specified number, and appends it to the previous string. Finally, when a lowercase letter is encountered, it's appended to the current string.
Step-by-Step Algorithm
- Step 1: Initialize an empty stack to store pairs of (string, count), an integer to store the current number, and a string to store the current decoded string segment.
- Step 2: Iterate through the input string character by character.
- Step 3: If the current character is a digit, update the current number by multiplying the existing number by 10 and adding the digit.
- Step 4: If the current character is an opening bracket '[', push the current string and number onto the stack, reset the current string to an empty string, and reset the current number to 0.
- Step 5: If the current character is a closing bracket ']', pop the last string and number from the stack. Repeat the current string 'number' times and concatenate it with the popped string. Update the current string with this concatenated string.
- Step 6: If the current character is a letter, append it to the current string.
- Step 7: After iterating through the entire string, the final decoded string will be stored in the current string. Return the current string.
Key Insights
- Insight 1: The nested nature of the encoded string suggests the use of a stack to keep track of the current string and repetition count.
- Insight 2: When encountering a ']', we need to retrieve the previous string and repetition count from the stack to decode the current segment.
- Insight 3: Numbers can be multi-digit, so accumulate digits until a non-digit character is encountered.
Complexity Analysis
Time Complexity: O(N*M)
Space Complexity: O(N*M)
Topics
This problem involves: String, Stack, Recursion.
Companies
Asked at: Activision, Agoda, ByteDance, Cisco, Compass, Coupang, Flexport, Geico, Goldman Sachs, Hertz, Huawei, Intuit, Mountblue, Nutanix, Ola Cabs, Ozon, Roku, Salesforce, Splunk, Sumo Logic, Tencent, Tesco, Tinkoff, Walmart Labs, Wix, Yelp, Zoho, eBay, razorpay.