Greedily try all possible differences
Approach\n  Greedy Approach\n\n# Intuition \nSee, we need to make the smallest lexicographical string right? which means the characters from beginning must be a
# Approach\n  Greedy Approach\n\n# Intuition \nSee, we need to make the smallest lexicographical string right? which means the characters from beginning must be as minimal or smaller as possible by taking care of k value as well.\n\nRight? It means the character from beginning must be starts with something similar to this pattern -> \n\n\'a\',\n\'b\',\n\'c\',\n\'d\',\n..\n..\n..\n\'z\'\n\nI hope till now you are able to follow me!\n\nSo by observing this pattern, now I\'m sure that okay i can try taking the difference of current character with all the characters from \'a\' to \'z\' why? \n\nBecause it is in a sorted manner, which means if we follow this sequence then we will be able to generate lexicographically smallest sequence, isn\'t it?\n\nSo, our job is done now! Compare current character with all the alphabets and check the difference if the difference is less than 14 then we dont need to do anything, but if the difference in greater than 13 then we need to cyclically calculate then only we can get the minimum character so for that we need to subtract 26 - difff.\n\nLet me explain you practically:\nchar -> \'b\' , k = 4\n\'b\' - \'a\' -> 1\n\nbut :\nchar -> \'z\' , k = 4\n\'z\' - \'a\' -> 25 but cyclically it should give 1 so for that we need to subtract 26-25 if the difference is greater than 13.\n\nAnd in the end we need to return modified string.\n\nHope you got the clarity about this greedy approach :)\n\n\n# Code\n```java []\nclass Solution {\n    public int reducerFunction(char[]str,char ch,int index,int k){\n        for(char chr=\'a\';chr<=\'z\';chr++){\n        int diff = ch-chr;\n        if(diff>13) diff = 26-diff;\n        if(diff<=k){\n        str[index]=chr;\n        return diff;\n        }\n        }\n        return 0;\n        }\n        public String getSmallestString(String s,int k){\n        char []str = s.toCharArray();\n        for(int i=0;i<str.length;i++){\n        if(k<=0) break;\n        int reduced = reducerFunction(str,str[i],i,k);\n        k-=reduced;\n        }\n        return new String(str);\n        }\n\n    }\n\n```
['String', 'Greedy', 'Java']