You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.

You can apply either of the following two operations any number of times and in any order on s:


	Add a to all odd indices of s (0-indexed). Digits post 9 are cycled back to 0. For example, if s = "3456" and a = 5, s becomes "3951".
	Rotate s to the right by b positions. For example, if s = "3456" and b = 1, s becomes "6345".


Return the lexicographically smallest string you can obtain by applying the above operations any number of times on s.

A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. For example, "0158" is lexicographically smaller than "0190" because the first position they differ is at the third letter, and '5' comes before '9'.

&nbsp;
Example 1:

Input: s = "5525", a = 9, b = 2
Output: "2050"
Explanation: We can apply the following operations:
Start:  "5525"
Rotate: "2555"
Add:    "2454"
Add:    "2353"
Rotate: "5323"
Add:    "5222"
Add:    "5121"
Rotate: "2151"
​​​​​​​Add:    "2050"​​​​​​​​​​​​
There is no way to obtain a string that is lexicographically smaller then "2050".


Example 2:

Input: s = "74", a = 5, b = 1
Output: "24"
Explanation: We can apply the following operations:
Start:  "74"
Rotate: "47"
​​​​​​​Add:    "42"
​​​​​​​Rotate: "24"​​​​​​​​​​​​
There is no way to obtain a string that is lexicographically smaller then "24".


Example 3:

Input: s = "0011", a = 4, b = 2
Output: "0011"
Explanation: There are no sequence of operations that will give us a lexicographically smaller string than "0011".


&nbsp;
Constraints:


	2 &lt;= s.length &lt;= 100
	s.length is even.
	s consists of digits from 0 to 9 only.
	1 &lt;= a &lt;= 9
	1 &lt;= b &lt;= s.length - 1


'''
w: BFS
h: for each possible number (node), we have two possible operations (add, rotate)
    it seems to be a 2^100 possible number, however, note:
    1) add a to number of odd index, we will get to the same number after 10 rounds of add
    2) s has even length, if b is odd, we can get the same number after n round
    3) for each shift, we would get different number at even index in 10 rounds
    
    so we would have 10 * n * 10 number at most, then we can use BFS + memo
'''
import collections

class Solution:
    def findLexSmallestString(self, s: str, a: int, b: int) -> str:
        seen = set()
        deque = collections.deque([s])

        while deque:
            #print(deque)
            curr = deque.popleft()
            seen.add(curr)
            
            #1.add
            ad = self.add_(curr, a)
            if ad not in seen:
                deque.append(ad)
                seen.add(ad)

            
            #2. rotate:
            ro = self.rotate_(curr, b)
            if ro not in seen:
                deque.append(ro)
                seen.add(ro)

        return min(seen)
        
        
    def add_(self,s,a):
        res = ''
        for idx, i in enumerate(s):
            if idx % 2 == 1:
                num = (int(i) + a) % 10
                res += str(num)
            else:
                res += i
                
        return res
    
    
    def rotate_(self, s, b):
        idx = len(s)-b
        res = s[idx:] + s[0:idx]
        return res
class Solution {
    private String result;
    public String findLexSmallestString(String s, int a, int b) {
        result = "z";
        HashSet<String> set = new HashSet<>();
        dfs(s, a, b, set);
        return result;
    }
    private void dfs(String s, int a, int b, HashSet<String> set) {
        if(set.contains(s))
            return;
        set.add(s);
        String s1, s2;
        s1 = addA(s, a);
        s2 = rotateB(s, b);
        dfs(s1, a , b, set);
        dfs(s2, a , b, set);
    }
    private String addA(String s, int a) {
        char c[] = s.toCharArray();
        int i, temp;
        for(i=1;i<s.length();i+=2) {
            temp = c[i] - '0';
            temp += a;
            temp = temp % 10;
            c[i] = (char)(temp + '0');
        } 
        s = new String(c);
        if(result.compareTo(s) > 0)
            result = s;
        return s;
    }
    private String rotateB(String s, int b) {
        if(b < 0)
            b += s.length();
        b = b % s.length();
        b = s.length() - b;
        s = s.substring(b) + s.substring(0, b);
        if(result.compareTo(s) > 0)
            result = s;
        return s;
    }
}
class Solution {
public:
    void dfs(string &s, string &small, int a, int b, unordered_map<string, string> &memo) {
        if (memo.count(s)) return;
        string res = s, str = res;
        memo[s] = res;
        rotate(str.begin(), str.begin() + b, str.end()); // rotate current string
        dfs(str, small, a, b, memo);
        if (memo[str] < res) res = memo[str];
        
        for (int i = 0; i < 9; i++) { // or add it
            for (int j = 1; j < s.length(); j+=2)
                s[j] = '0' + ((s[j] - '0') + a) % 10;
            if (s < res) res = s;
            dfs(s, small, a, b, memo);
        }
        if (small > res) small = res;
        memo[s] = res;
    }
    string findLexSmallestString(string s, int a, int b) {
        unordered_map<string, string> memo;
        string res = s;
        dfs(s, res, a, b, memo);
        return  res;
    }
};
var findLexSmallestString = function(s, a, b) {
    const n = s.length;
    const visited = new Set();
    const queue = [];

    visited.add(s);
    queue.push(s);

    let minNum = s;

    while (queue.length > 0) {
        const currNum = queue.shift();

        if (currNum < minNum) minNum = currNum;

        const justRotate = rotate(currNum);
        const justAdd = add(currNum);

        if (!visited.has(justRotate)) {
            visited.add(justRotate);
            queue.push(justRotate);
        }

        if (!visited.has(justAdd)) {
            visited.add(justAdd);
            queue.push(justAdd);
        }
    }

    return minNum;

    function rotate(num) {
        let rotatedNum = "";
        const start = n - b;

        for (let i = 0; i < b; i++) {
            rotatedNum += num.charAt(start + i);
        }

        const restDigs = num.substring(0, n - b);
        rotatedNum += restDigs;

        return rotatedNum;
    }

    function add(num) {
        let nextNum = "";

        for (let i = 0; i < n; i++) {
            let currDig = num.charAt(i);

            if (i % 2 == 0) {
                nextNum += currDig;
            }
            else {
                let newDig = (parseInt(currDig) + a) % 10;
                nextNum += newDig;
            }
        }

        return nextNum;
    }
};
Lexicographically Smallest String After Applying Operations