🚀✅ || Image representation || Steps Explained || Beginner friendly || JAVA
Intuition\n Describe your first thoughts on how to solve this problem. \nGiven in the question, we need to find a non-empty substring by which we can perform th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGiven in the question, we need to find a non-empty substring by which we can perform the given operation.\nLet\'s think about that how can we get a lexicographically smallest substring?\nThe answer to this is we need to get the non-empty substring at the left most possible in the string.\n\nCan we select the character \'a\' in our substring ?\nIf we select it, then it will be replaced by \'z\' which is way opposite to lexicographically smallest substring.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nLet\'s assume that the array is bounded with the char \'a\' on the left and right outside of the array.\n![array.jpg](https://assets.leetcode.com/users/images/fb1dd359-c1a4-481f-8892-1dfe42760ef7_1686457539.3296816.jpeg)\n\nThe approach is that we need to a substring before the \'a\' or in between two a\'s if they are present more than one time in the string, otherwise we can simply simply replace all the characters if \'a\' is not present in the string.\n\nSteps:\n\n1. first step is to find the left most character which is not \'a\'.\n2. if all the chars in the string are \'a\' then we need to perform the operation on the last char since the selected substring should be non-empty.\n3. perform the operations to the substring in between two a\'s.  \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n```java []\nclass Solution {\n    public String smallestString(String s) {\n        \n        char[] c=s.toCharArray();\n        \n        int i = 0;\n        while (i < s.length() && c[i] == \'a\') //step-1\n        {\n            i++;\n        }\n        \n\n        if (i == s.length()) //step-2\n        {\n            c[s.length() - 1] = \'z\';\n        }\n        else\n        {\n            while (i < s.length() && c[i] != \'a\') //step-3\n            {\n                c[i]=(char)(c[i]-1);\n                i++;\n            }\n        }\n\n        \n        String t=String.valueOf(c);\n        \n        return t;\n        \n    }\n}\n```\n\n\n
['Array', 'Two Pointers', 'Java']