Watch 10 video solutions for Decoded String at Index, a medium level problem involving String, Stack. This walkthrough by codestorywithMIK has 17,115 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an encoded string s. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:
d, the entire current tape is repeatedly written d - 1 more times in total.Given an integer k, return the kth letter (1-indexed) in the decoded string.
Example 1:
Input: s = "leet2code3", k = 10 Output: "o" Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode". The 10th letter in the string is "o".
Example 2:
Input: s = "ha22", k = 5 Output: "h" Explanation: The decoded string is "hahahaha". The 5th letter is "h".
Example 3:
Input: s = "a2345678999999999999999", k = 1 Output: "a" Explanation: The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a".
Constraints:
2 <= s.length <= 100s consists of lowercase English letters and digits 2 through 9.s starts with a letter.1 <= k <= 109k is less than or equal to the length of the decoded string.263 letters.Problem Overview: The string s contains letters and digits. Letters append directly to a decoded string, while digits repeat the entire current decoded sequence d-1 more times. The decoded string can become extremely large, so you cannot build it directly. The task is to return the kth character of the final decoded string.
Approach 1: Iterative Length Expansion Simulation (O(n) time, O(1) space)
Scan the string from left to right and track the length of the decoded string without actually constructing it. When you encounter a letter, increment the length. When you encounter a digit d, multiply the current length by d because the entire sequence repeats. Once the computed length reaches or exceeds k, you know the answer lies within the prefix processed so far. This approach models the decoding process mathematically using string traversal rather than allocating memory for the decoded string.
Approach 2: Decode Length Backtracking (O(n) time, O(1) space)
This is the most common optimal solution. First pass: compute the total decoded length using the same expansion logic. Second pass: traverse the string backward and reduce k relative to the current decoded length. When you hit a digit, divide the length by that digit because you are stepping back through the repeated blocks. When you hit a letter, check if k == 0 or k == length. If true, that character is the answer. The key insight is that the decoded string forms repeating segments, so modulo arithmetic lets you map k back into earlier segments.
Approach 3: Reverse Decoding Simulation (O(n) time, O(1) space)
This variation also processes the string backward but focuses on simulating how the index shrinks as repetition layers are removed. Each digit compresses the decoded length by dividing it, while letters decrement the length by one. If the adjusted index matches the current position, you return that character. This technique avoids recursion and keeps only a few integer variables, making it efficient for very large decoded lengths.
Approach 4: Iterative Length Calculation with Stack Insight (O(n) time, O(1) space)
Although no explicit stack is required, the algorithm behaves like unwinding nested expansions. Each digit acts like pushing a repetition frame, and the reverse traversal pops those frames while shrinking the search index. Understanding this structure helps when reasoning about nested repetitions and aligns conceptually with problems involving stack-like backtracking over encoded sequences.
Recommended for interviews: The Decode Length Backtracking approach is what interviewers expect. It demonstrates that you recognize the decoded string may exceed memory limits and that you can reason about the structure mathematically. Explaining why constructing the string is infeasible shows good problem analysis, while the backward traversal with modulo arithmetic proves strong algorithmic thinking with string processing.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Length Expansion Simulation | O(n) | O(1) | When you want to model decoded length without building the string |
| Decode Length Backtracking | O(n) | O(1) | Best general solution and most expected in interviews |
| Reverse Decoding Simulation | O(n) | O(1) | When implementing a clean backward traversal without extra structures |
| Iterative Length Calculation | O(n) | O(1) | When reasoning about nested repetitions similar to stack unwinding |