Decoded String at Index - Complete Solution Guide
Decoded String at Index is LeetCode problem 880, 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
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: If the character read is a letter, that letter is written onto the tape. If the character read is a digit d , the entire current tape is repeatedly written d - 1 more times in total. Given an integer k , return the k th letter ( 1-indexed) in the decoded string . Example 1: Input: s = "leet2code3", k = 10 Output: "o" Explanation: The decoded st
Detailed Explanation
The problem asks us to decode a string that is encoded with letters and digits. Letters are added directly to the decoded string. Digits represent repeating the current decoded string a number of times equal to the digit. Given an encoded string `s` and an index `k`, we must find the character at the `k`-th position in the fully decoded string (1-indexed). The problem ensures the decoded string's length is within reasonable bounds (less than 2^63) and `k` is always a valid index within the decoded string.
Solution Approach
The provided solution uses a reverse iteration approach. First, it calculates the total size of the decoded string by iterating through the encoded string. Then, it iterates backward through the encoded string. If a digit is encountered, it implies a repetition. We update `size` and `k` based on the repetition. If a letter is encountered, we check if `k` is equal to the current `size`. If they're equal, it means the current letter is at the `k`-th position, so we return it.
Step-by-Step Algorithm
- Step 1: Calculate the size of the decoded string. Iterate through the encoded string `s`. If the character is a digit, multiply `size` by the digit. Otherwise, increment `size` by 1.
- Step 2: Iterate through the encoded string `s` in reverse order.
- Step 3: If the current character is a digit, divide `size` by the digit, and update `k` to `k % size`. If `k` becomes 0, set it to `size` to account for the edge case where `k` is a multiple of the current string length.
- Step 4: If the current character is a letter, check if `k` is equal to `size`. If it is, then the current character is the answer and return it.
- Step 5: If the current character is a letter and `k` is not equal to `size`, decrement `size`.
Key Insights
- Insight 1: Instead of building the entire decoded string (which could be huge and cause memory issues), we can work backward to find the character at the k-th position.
- Insight 2: The crucial idea is to keep track of the size of the decoded string and use the modulo operator (%) to simulate the repetitions and find the original position that contributes to the k-th character.
- Insight 3: If we encounter a digit while traversing backwards, we divide the `size` by that digit and take `k % size` to simulate moving back within the repeated section. If `k % size == 0`, it means `k` is actually `size` in this repetition.
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(1)
Topics
This problem involves: String, Stack.
Companies
Asked at: National Instruments, PhonePe.