<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 {
public:
    string findLexSmallestString(string s, int a, int b){
        //s is only 100 chars
        //brute force -> 100 *(2, if b is odd) * ~9 * 50(to change each char)
        //should be fine
        vector<bool> rotated(s.size(), false);
        int pos = 0;
        bool evensAlso = b%2 ==1;
        addOdds(s, a, evensAlso);
        string rec = s;
        while(rotated[pos] == false){
            rotated[pos] = true;
            pos = (pos + b) % s.size();
            s = rotate(s, b);
            s = addOdds(s, a, evensAlso);
            rec = min(rec, s);
            // cout << s << endl;
        }
        return rec;
    }

    string rotate(string s, int b){ //rotate right by b positions
        string temp = s.substr(s.size()-b, b);
        s = temp + s.substr(0, s.size()-b);
        return s;
    }
    string addOdds(string s, int a, bool alsoEvens){
        string rec = s;
        vector<bool> added(10, false);
        while(added[s[1]] == false){
            added[s[1]] = true;
            for(int i=1; i<s.size(); i+=2){
                s[i] = '0' + (((s[i]-'0') + a) % 10);
            }
            rec = min(s, rec);
        }
        s=rec;
        if(alsoEvens){
            added = vector<bool>(10, false);
            while(added[s[0]] == false){
                added[s[0]] = true;
                for(int i=0; i<s.size(); i+=2){
                    s[i] = '0' + (((s[i]-'0') + a) % 10);
                }
                rec = min(s, rec);
            }
        }
        return rec;
    }

/*
    string getOpt(string half, int a){
        int mult = 0;
        int opt = half[0]-'0';
        for(int k=1; k<=9; k++){
            if((a * k + half[0]-'0') % 10 < opt){
                opt = (a * k + half[0]-'0') % 10;
                mult = k;
            }
        }
        //found optimal coefficient of a
        for(int i=0; i<half.size(); i++){
            half[i] = '0' + (a*mult + half[i] - '0')%10;
        }
        return half;

    }
    string findLexSmallestString(string s, int a, int b, bool once = true) {
        // return getOpt(s, a);
        //look at odd / even indices separately
        //hm...brute force :/
        string ans = s;
        if(b%2==1 && once){
            ans = min(s, findLexSmallestString(s.back()+s.substr(0, s.size()-1),a,b,false));
        }
        // if(once)
        //     cout << "======"<< ans << endl;

        string first = "";
        string second = "";
        //split string
        for(int i=0; i<s.size(); i+=2){
            first += s[i];
            second += s[i+1];
        }
        // cout << first << ", " << second << endl;
        //try different rotations
        for(int i=0; i<s.size(); i++){
            //rotate 1
            if(i!=0){
                first = first.back() + first.substr(0, first.size()-1);
                second = second.back() + second.substr(0, first.size()-1);
            }
            // cout << first << ", " << second << endl;
            string temp = getOpt(first,a);
            string temp2 = getOpt(second,a);
            if(b%2==0){
                temp = first; //no rotation to it possible
            }
            string comp = "";
            for(int k=0; k<temp.size(); k++)
                comp += (string(1,temp[k]) + temp2[k]);
            // cout << comp << endl;
            ans = min(ans, comp);
            
        }
        return ans;
    }
    */
};
10745