[Java] OOP approach with DFS, Beats 98.28%, Easy to understand
Approach\n- We\'ll represent each state of the string s as a custom class State.\n  - We need the State class to work in conjunction with Set. Namely, to overri
# Approach\n- We\'ll represent each state of the string s as a custom class `State`.\n  - We need the `State` class to work in conjunction with Set<State>. Namely, to override equals and hashcode. Our optimization is hashcode calculation during object creation\n- We\'ll use Depth-First Search (DFS) to explore all possible states.\n- At each step, we\'ll generate two new states:\n    1. Adding a to all odd indices.\n    2. Rotating the string to the right by b positions.\n- We\'ll keep track of visited states to avoid revisiting them.\n- We\'ll compare each state with the minimum state found so far and update if necessary.\n\n# Code\n```\nclass Solution {\n\n   \n \n    public String findLexSmallestString(String s, int a, int b) {\n        // Initialize the initial state and a set to keep track of visited states\n        var baseState = new State(s);\n        var set = new HashSet<State>();\n\n        // Generate states recursively and find the minimum\n        var answer = findLexSmallestState(a, b, baseState, set);\n\n        // Return the lexicographically smallest string\n        return new String(answer.chars);\n    }\n\n    // Recursive method to generate states\n    public State findLexSmallestState(int a, int b, State currentState, Set<State> states) {\n        // If the state is already visited, return it\n        if (states.contains(currentState)) {\n            return currentState;\n        }\n        states.add(currentState);\n\n        // Generate two new states: one by adding \'a\' to odd indices, and another by rotating right by \'b\'\n        var state1 = findLexSmallestState(a, b, currentState.addToOddIndexes(a), states);\n        var state2 = findLexSmallestState(a, b, currentState.rotateToRight(b), states);\n\n        // Return the minimum state among the current state and the generated states\n        return currentState.min(state1).min(state2);\n    }\n\n    // Custom class to represent states of the string\n    public static class State {\n        final char[] chars;\n        final int cachedHashCode;\n\n        public State(char[] chars) {\n            this.chars = chars;\n            this.cachedHashCode = Arrays.hashCode(chars);\n        }\n\n        public State(String s) {\n            this.chars = s.toCharArray();\n            this.cachedHashCode = Arrays.hashCode(chars);\n        }\n\n        // Method to find the minimum state\n        public State min(State state) {\n            if (Arrays.compare(chars, state.chars) > 0) {\n                return state;\n            }\n            return this;\n        }\n\n        // Method to add \'a\' to odd indices\n        public State addToOddIndexes(int a) {\n            var newChars = Arrays.copyOf(chars, chars.length);\n            for (int i = 1; i < chars.length; i += 2) {\n                newChars[i] = (char) ((newChars[i] - \'0\' + a) % 10 + \'0\');\n            }\n            return new State(newChars);\n        }\n\n        // Method to rotate the string to the right by \'b\' positions\n        public State rotateToRight(int b) {\n            var newChars = new char[chars.length];\n            for (int i = 0; i < chars.length; i++) {\n                newChars[(i + b) % chars.length] = chars[i];\n            }\n            return new State(newChars);\n        }\n\n        @Override\n        public int hashCode() {\n            return cachedHashCode;\n        }\n\n        @Override\n        public boolean equals(Object obj) {\n            return Arrays.equals(chars, ((State) obj).chars);\n            // The probability of a collision is low and we can do such optimization, but this is exclusively for tests\n            // DON\'T USE IT IN PRODUCTION\n            // return cachedHashCode == obj.hashCode();\n        }\n        \n    }\n}\n```\n# Ps:\n- Often, the OOP approach falls behind in performance compared to more optimized solutions. However, in this problem, applying such an approach significantly enhances code comprehension and readability, while still achieving a respectable 98% success rate.\n- Even though variables like "i", "a", "b", "s", etc., are present in the solution, I\'ve retained them for easier understanding based on the problem\'s context. However, it\'s advisable to avoid such practices in production code.\n- For method comments we should use documentation comments and are delimited by /**...*/\n- In production, strive to minimize the creation of custom methods in favor of utilizing built-in/library functions whenever possible.\n#### Feel free to leave comments as this is my first post on LeetCode, and I\'m open to feedback!\n
['String', 'Hash Function', 'Java']