Java 80% Beats| Easy Understanding | explanation of case "a...a"
Intuition\n> POINTS to be NOTED first :\nGiven : Select any non-empty substring(not a subsequence) of s, possibly the entire string, then replace each one of it
# Intuition\n> POINTS to be NOTED first :\nGiven : Select any ***`non-empty`*** ***`substring`(not a subsequence)*** of s, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet.\nperforming the above operation `exactly once.`\n\nI Intially thought\nAt any situation we should not change the \'a\' as it becomes \'z\' and it becomes lexicographically wrong answer,\nbut changed my thought on seeing the i/o for string "aa....aa"\n\n# Approach\nAlso point to be mentioned is using an Array(charArray) instead of string modifications as you can see it in comments.This string modificatns case takes TLE problem on case 2237/2241. So watching some solutions using this string to array method, I have modified it .\n\nCases : \n1. We have not encounterd \'a\' then we will change all chars as per the condition. And making `modified` flag as true. This flag helps us to tackle the situation when we encounter \'a\' in middle(case 2).\n2. We have modified the string and have encountered a \'a\' (the string is not starting with \'a\' but have an \'a\' in middle) then we are breaking the loop because w.k.t we are not changing the \'a\' as it changes to \'z\' and we already modified the substring of s which is nonempty.\n\nAnd the if case (done actually for the case if string s = "aa....aa") : if(modified == false) is because we have to select a nonempty substring . In this cae our general loop will return again the same string with no modifications.but its not above condtn so we are changing the last char such that is smallest of all possiblities with nonempty selection of substring.\n\n# Complexity\n- Time complexity, Space complexity:$$O(n)$$\n# Code\n```\nclass Solution {\n    public String smallestString(String s) {\n        boolean modified = false;\n       char ch [] = s.toCharArray();\n       // char changer;//changing the present char to its prev char\n        int i;\n        for(i =0;i< s.length();i++)\n        {\n            //in place of ch[i] we have to use s.charAt(i)\n            if(ch[i] == \'a\' && modified) break;\n            else if(ch[i] != \'a\') \n            {\n               ch[i] =(char)(ch[i]-1);    \n                // s = s.substring(0,i)+changer+s.substring(i+1,s.length());\n                modified = true;\n            }\n        }\n        if(modified == false)\n        {\n            ch[s.length()-1] = \'z\';\n            // return s.substring(0,s.length()-1) + \'z\';\n        }\n        //return s;\n        return String.valueOf(ch);\n    }\n}\n```
['String', 'C', 'Python', 'C++', 'Java']