Basic DFS - Brute force | This kind of questions = DFS
Basic DFS - Brute force\n\nFor each string, just try rotate as well as add operations. It is possible that you had thoght that the brute force approach wouldn\'
Basic DFS - Brute force\n\nFor each string, just try rotate as well as add operations. It is possible that you had thoght that the brute force approach wouldn\'t work, doing some calculations really help in this case to prove whether it will work or not.\n\n**Thanks!**\nPlease upvote if the code helps! It encourages a lot...\n\n```\n\nclass Solution {\n    unordered_set<string> visited;\n    string ans;\n    int a, b;\n    int n;\n    \n    bool isVisited(string s) {\n        return visited.find(s) != visited.end();\n    }\n    \n    void visit(string s) {\n        ans = min(ans, s);\n        visited.insert(s);\n    }\n    \n    string rotate(string s, int x) {\n        reverse(s.begin(), s.end());\n        reverse(s.begin(), s.begin()+x);\n        reverse(s.begin()+x, s.end());\n        return s;\n    }\n    \n    string add(string s, int x) {\n        for (int i=1; i<n; i += 2) {\n            char &c = s[i];\n            c = \'0\' + (c-\'0\'+x)%10;\n        }\n        return s;\n    }\n    \n\npublic:\n    \n    void dfs(string s) {\n        if (isVisited(s)) {\n            return ;\n        }\n        visit(s);\n        dfs(rotate(s, b));\n        dfs(add(s, a));\n    }\n    \n    string findLexSmallestString(string s, int a, int b) {\n        ans = s;\n        this->a = a;\n        this->b = b;\n        n = s.size();\n        dfs(s);\n        return ans;\n    }\n};\n```\n\n**Some of my other posts**\n1. Leetcode 1627: https://leetcode.com/problems/graph-connectivity-with-threshold/discuss/899462\n2. Leetcode 1626: https://leetcode.com/problems/best-team-with-no-conflicts/discuss/899475\n3. Leetcode 1625: https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/discuss/899489\n4. Leetcode 1600: https://leetcode.com/problems/throne-inheritance/discuss/866400\n5. Leetcode 1599: https://leetcode.com/problems/maximum-profit-of-operating-a-centennial-wheel/discuss/866375\n6. Leetcode 1584: https://leetcode.com/problems/min-cost-to-connect-all-points/discuss/844270\n7. Leetcode 1583: https://leetcode.com/problems/count-unhappy-friends/discuss/843928\n8. Leetcode 1579: https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/discuss/831506\n9. Leetcode 1578: https://leetcode.com/problems/minimum-deletion-cost-to-avoid-repeating-letters/discuss/831533\n10. Leetcode 1577: https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/discuss/831548\n11. Leetcode 1553: https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/794075\n12. Leetcode 1545: https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/discuss/781105\n13. Leetcode 1536: https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/discuss/768076\n14. Leetcode 1535: https://leetcode.com/problems/find-the-winner-of-an-array-game/discuss/767958\n15. Leetcode 1534: https://leetcode.com/problems/count-good-triplets/discuss/768170\n16. Leetcode 1529: https://leetcode.com/problems/bulb-switcher-iv/discuss/755939\n17. Leetcode 1503: https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/discuss/720313\n18. Leetcode 1488: https://leetcode.com/problems/avoid-flood-in-the-city/discuss/697687\n19. Leetcode 1487: https://leetcode.com/problems/making-file-names-unique/discuss/719962\n20. Leetcode 1472: https://leetcode.com/problems/design-browser-history/discuss/674486\n21. Leetcode 1471: https://leetcode.com/problems/the-k-strongest-values-in-an-array/discuss/674346\n22. Leetcode 1466: https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/discuss/661710\n23. Leetcode 1465: https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/discuss/661995\n24. Leetcode 1458: https://leetcode.com/problems/max-dot-product-of-two-subsequences/discuss/648528\n25. Leetcode 1443: https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/discuss/623673\n26. Leetcode 1436: https://leetcode.com/problems/destination-city/discuss/609874\n27. Leetcode 1424: https://leetcode.com/problems/diagonal-traverse-ii/discuss/597741\n28. Leetcode 1423: https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/597825\n29. Leetcode 1419: https://leetcode.com/problems/minimum-number-of-frogs-croaking/discuss/586653
[]