<p>You are given an encoded string <code>s</code>. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:</p>

<ul>
	<li>If the character read is a letter, that letter is written onto the tape.</li>
	<li>If the character read is a digit <code>d</code>, the entire current tape is repeatedly written <code>d - 1</code> more times in total.</li>
</ul>

<p>Given an integer <code>k</code>, return <em>the </em><code>k<sup>th</sup></code><em> letter (<strong>1-indexed)</strong> in the decoded string</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;leet2code3&quot;, k = 10
<strong>Output:</strong> &quot;o&quot;
<strong>Explanation:</strong> The decoded string is &quot;leetleetcodeleetleetcodeleetleetcode&quot;.
The 10<sup>th</sup> letter in the string is &quot;o&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;ha22&quot;, k = 5
<strong>Output:</strong> &quot;h&quot;
<strong>Explanation:</strong> The decoded string is &quot;hahahaha&quot;.
The 5<sup>th</sup> letter is &quot;h&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;a2345678999999999999999&quot;, k = 1
<strong>Output:</strong> &quot;a&quot;
<strong>Explanation:</strong> The decoded string is &quot;a&quot; repeated 8301530446056247680 times.
The 1<sup>st</sup> letter is &quot;a&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= s.length &lt;= 100</code></li>
	<li><code>s</code> consists of lowercase English letters and digits <code>2</code> through <code>9</code>.</li>
	<li><code>s</code> starts with a letter.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
	<li>It is guaranteed that <code>k</code> is less than or equal to the length of the decoded string.</li>
	<li>The decoded string is guaranteed to have less than <code>2<sup>63</sup></code> letters.</li>
</ul>

# Tag: String, recursion, mod, circular

# Create Date: 2024/3/9
# Review Date: [2024/3/16, 2024/4/9, 2024/5/9, 2024/6/9, 2024/7/11]

"""
Ref. https://www.youtube.com/watch?v=6iE7QJRMXzs

1 2 3 4 8 9 10 11 12 36
l e e t 2 c o  d  e  3
储存以当前char结尾时候的decoded str的长度。
每次碰到digit就按倍数扩增。
往回看的时候就按倍数递减，
! 同时update k。注意是按1-index的。
"""
class Solution:
    def decodeAtIndex(self, s: str, k: int) -> str:
        length = 0
        ds_set = set([str(v) for v in range(2, 10)])
        for i in range(len(s)):
            if s[i] not in ds_set:
                length += 1
            else:
                length *= int(s[i])
        
        for i in range(len(s) - 1, -1, -1):
            if s[i] in ds_set:
                length //= int(s[i])
                # ! update k是关键。而且如果是按1 index，取模要多一步减1之后加回来
                # (Note, 这个k的update放到else里面也行，都一样，都是在用之前mod)
                if length < k:
                    k -= 1
                    k = int(k % length)
                    k += 1
            else:
                if length == k:
                    return s[i]
                length -= 1
        
        return ""
        
16400