👍Runtime 3 ms  Beats 100.00% of users with Java
Code\n\nclass Solution {\n    public String findLexSmallestString(String s, int a, int b) {\n        if (a == 3 || a == 7 || a == 9) {\n            a = 1;\n    
# Code\n```\nclass Solution {\n    public String findLexSmallestString(String s, int a, int b) {\n        if (a == 3 || a == 7 || a == 9) {\n            a = 1;\n        }\n        if (a == 4 || a == 6 || a == 8) {\n            a = 2;\n        }\n        // a in [1,2,5] now\n\n        String smallest = s;\n        boolean[] processed = new boolean[s.length()];\n        Arrays.fill(processed, false);\n        for (int i = 0; !processed[i % s.length()]; i += b) {\n            int index = i % s.length();\n            processed[index] = true;\n            String cur = minimizing(s.substring(s.length() - index) + s.substring(0, s.length() - index), a, b % 2 == 1);\n            if (cur.compareTo(smallest) < 0) {\n                smallest = cur;\n            }\n        }\n        return smallest;\n    }\n\n    public String minimizing(String original, int step, boolean changePrev) {\n        char[] inChars = original.toCharArray();\n        int minus = 0;\n        int minusPrev = 0;\n        switch (step) {\n            case 1:\n                minus = inChars[1] - \'0\';\n                minusPrev = inChars[0] - \'0\';\n                break;\n            case 2:\n                minus = inChars[1] - \'0\';\n                minusPrev = inChars[0] - \'0\';\n                if (minus % 2 == 1) {\n                    minus--;\n                }\n                if (minusPrev % 2 == 1) {\n                    minusPrev--;\n                }\n                break;\n            case 5:\n                if (inChars[1] >= \'5\') {\n                    minus = 5;\n                }\n                if (inChars[0] >= \'5\') {\n                    minusPrev = 5;\n                }\n                break;\n        }\n        for (int i = 1; i < inChars.length; i += 2) {\n            inChars[i] -= minus;\n            if (inChars[i] < \'0\') {\n                inChars[i] += 10;\n            }\n            if (changePrev) {\n                inChars[i - 1] -= minusPrev;\n                if (inChars[i - 1] < \'0\') {\n                    inChars[i - 1] += 10;\n                }\n            }\n        }\n        return new String(inChars);\n    }\n\n}\n```
['Java']