<p>You are given a string <code>s</code> of <strong>even length</strong> consisting of digits from <code>0</code> to <code>9</code>, and two integers <code>a</code> and <code>b</code>.</p>

<p>You can apply either of the following two operations any number of times and in any order on <code>s</code>:</p>

<ul>
	<li>Add <code>a</code> to all odd indices of <code>s</code> <strong>(0-indexed)</strong>. Digits post <code>9</code> are cycled back to <code>0</code>. For example, if <code>s = &quot;3456&quot;</code> and <code>a = 5</code>, <code>s</code> becomes <code>&quot;3951&quot;</code>.</li>
	<li>Rotate <code>s</code> to the right by <code>b</code> positions. For example, if <code>s = &quot;3456&quot;</code> and <code>b = 1</code>, <code>s</code> becomes <code>&quot;6345&quot;</code>.</li>
</ul>

<p>Return <em>the <strong>lexicographically smallest</strong> string you can obtain by applying the above operations any number of times on</em> <code>s</code>.</p>

<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears earlier in the alphabet than the corresponding letter in <code>b</code>. For example, <code>&quot;0158&quot;</code> is lexicographically smaller than <code>&quot;0190&quot;</code> because the first position they differ is at the third letter, and <code>&#39;5&#39;</code> comes before <code>&#39;9&#39;</code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;5525&quot;, a = 9, b = 2
<strong>Output:</strong> &quot;2050&quot;
<strong>Explanation:</strong> We can apply the following operations:
Start:  &quot;5525&quot;
Rotate: &quot;2555&quot;
Add:    &quot;2454&quot;
Add:    &quot;2353&quot;
Rotate: &quot;5323&quot;
Add:    &quot;5222&quot;
Add:    &quot;5121&quot;
Rotate: &quot;2151&quot;
Add:    &quot;2050&quot;​​​​​
There is no way to obtain a string that is lexicographically smaller than &quot;2050&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;74&quot;, a = 5, b = 1
<strong>Output:</strong> &quot;24&quot;
<strong>Explanation:</strong> We can apply the following operations:
Start:  &quot;74&quot;
Rotate: &quot;47&quot;
​​​​​​​Add:    &quot;42&quot;
​​​​​​​Rotate: &quot;24&quot;​​​​​​​​​​​​
There is no way to obtain a string that is lexicographically smaller than &quot;24&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;0011&quot;, a = 4, b = 2
<strong>Output:</strong> &quot;0011&quot;
<strong>Explanation:</strong> There are no sequence of operations that will give us a lexicographically smaller string than &quot;0011&quot;.
</pre>

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

<ul>
	<li><code>2 &lt;= s.length &lt;= 100</code></li>
	<li><code>s.length</code> is even.</li>
	<li><code>s</code> consists of digits from <code>0</code> to <code>9</code> only.</li>
	<li><code>1 &lt;= a &lt;= 9</code></li>
	<li><code>1 &lt;= b &lt;= s.length - 1</code></li>
</ul>

class Solution:
    def findLexSmallestString(self, s: str, a: int, b: int) -> str:
        # if b is even, does not help 

        # if b is odd, we can just

        # 5 2 4 1
        # we can always make it 0

        # 0 7 9 6
        # 0 2 9 3

        # 2 3 5 8 9
        # 0 1 3 6 7
        # 8 9 2 3 5
        # 0 1 3 4 6

        seen = set()

        def dfs(s):
            if s in seen:
                return "ABCDEF"

            seen.add(s)
            
            tmp = ""
            for i in range(len(s)):
                if i % 2 == 0:
                    tmp += s[i]
                else:
                    tmp += str((int(s[i]) + a) % 10)

            return min(s, tmp, dfs(tmp), dfs(s[(len(s)-b):] + s[0:(len(s)-b)]))

        return dfs(s)

    #     cur_min = float('inf')
    #     odds_string = ""
    #     evens_string = ""

    #     for i in range(len(s)):
    #         if i % 2 != 0:
    #             odds_string += s[i]
            
    #         if i % 2 == 0:
    #             evens_string += s[i]

    #     min_odd = self.decrement_num(odds_string, int(odds_string[0]))
    #     min_even = self.decrement_num(evens_string, int(evens_string[0]))

    #     # 5525 
    #     # 0 -3 0

    #     # 0 0 

    #     for i in range(len(odds_string)):
    #         new_odd = odds_string[i:] + odds_string[0:i]
    #         new_even = evens_string[i:] + evens_string[0:i]

    #         new_odd = self.decrement_num(new_odd, int(new_odd[0]))
    #         new_even = self.decrement_num(new_even, int(new_even[0]))

    #         if int(new_odd) < int(min_odd):
    #             min_odd = new_odd
        
    #         if int(new_even) < int(min_even):
    #             min_even = new_even


    #     if b % 2 != 0:
    #         example = zip(min_even, min_odd)
    #     else:
    #         example = zip(evens_string, min_odd)

    #     sol = ""
    #     for a, b in example:
    #         sol += a
    #         sol += b

    #     return sol 
    #     # 5525
    #     # 5020
    #     # 2050

    # def decrement_num(self, s, times):
    #     # decrement every digit in the given number times times 
    #     sol = ""
    #     for i in s:
    #         c = int(i) - int(s[0])
    #         if c < 0:
    #             c = 10 - c

    #         sol += str(c)

    #     return sol 

38300