> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Decoded String at Index Python Solution

> Tested Python solution for LeetCode 880 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 880, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack). [View on LeetCode](https://leetcode.com/problems/decoded-string-at-index/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 880   # by problem number
lcpy gen -s decoded_string_at_index   # by problem name
```

## Problem

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.

### Examples

```
Input: s = "leet2code3", k = 10
Output: "o"
Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode".
The 10th letter in the string is "o".
```

```
Input: s = "ha22", k = 5
Output: "h"
Explanation: The decoded string is "hahahaha".
The 5th letter is "h".
```

```
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 \<= 100
* s consists of lowercase English letters and digits 2 through 9.
* s starts with a letter.
* 1 \<= k \<= 10^9
* It is guaranteed that k is less than or equal to the length of the decoded string.
* The decoded string is guaranteed to have less than 2^63 letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/decoded_string_at_index/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/decoded_string_at_index/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def decode_at_index(self, s: str, k: int) -> str:
        size = 0
        for char in s:
            size = size * int(char) if char.isdigit() else size + 1

        for char in reversed(s):
            k %= size
            if k == 0 and char.isalpha():
                return char
            if char.isdigit():
                size //= int(char)
            else:
                size -= 1
        raise ValueError("k out of range")
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.