> For the complete documentation index, see [llms.txt](https://code-render.gitbook.io/leetcode-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-render.gitbook.io/leetcode-editorials/09-september/27.decoded-string-at-index.md).

# 27- Decoded String at Index

The problem can be found at the following link: [Question Link](https://leetcode.com/problems/decoded-string-at-index/description/)

## My Approach

1. Initialize `decodedLength` to 0: This variable will keep track of the total length of the decoded string as we iterate through it. It's initially set to 0.
2. Iterate through each character in the input string `s` using a for-each loop:
   * If the character `c` is a digit (checked using `isdigit()`), it means we need to repeat the characters seen so far in the decoded string by a factor of `c - '0'` times (convert the character to an integer). So, update `decodedLength` by multiplying it by `c - '0'`.
   * If the character is not a digit, it represents a single character in the decoded string, so increment `decodedLength` by 1.
3. Reverse Iterate through the string `s` from the end to the beginning (right to left):
   * For each character `curr` at index `i`:
     * If `curr` is a digit, it means that the previous portion of the decoded string is repeated `curr - '0'` times. To find the effective length of the decoded string up to this point, divide `decodedLength` by `curr - '0'`, and update `decodedLength` accordingly.
     * Update `k` by taking its modulo with the current `decodedLength`. This ensures that `k` stays within the bounds of the current portion of the decoded string.
     * If `k` becomes 0 after the modulo operation or if `decodedLength` equals `k`, it means that `k` corresponds to the current character `curr`. In this case, return a string containing only `curr`.
4. If the loop completes without finding a character to return, return an empty string, indicating that the decoded string is not found within the given input parameters.

## Time and Auxiliary Space Complexity

* Time Complexity: `O(n)`
* Auxiliary Space Complexity: `O(1)`

## Code (C++)

```cpp

class Solution {
public:
    string decodeAtIndex(string s, int k) {
        long long decodedLength = 0;
        for(auto c : s){
            if(isdigit(c)){
                decodedLength = decodedLength * ( c - '0');
            }else{
                decodedLength++;
            }
        }

        for(int i = s.length() - 1; i >= 0; i--){
            char curr = s[i];
            if(isdigit(curr)){
                decodedLength = decodedLength/(curr - '0');
                k = k % decodedLength;
            }else{
                if( k == 0 || decodedLength == k){
                    return string(1,curr);
                }
                decodedLength--;
            }
        }

        return "";
    }
};

```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://leetcode.com/discuss/general-discussion). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [rishabhv12/Daily-Leetcode-Solution](https://github.com/rishabhv12/Daily-Leetcode-Solution) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://code-render.gitbook.io/leetcode-editorials/09-september/27.decoded-string-at-index.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
