1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public String smallestString(String s) { char[] chars = s.toCharArray(); boolean replaced = false; for (int i = 0; i < chars.length; i++) { if (chars[i] == 'a') { if (replaced) { break; } } else { chars[i]--; replaced = true; } }
if (!replaced) { chars[chars.length - 1] = 'z'; }
return new String(chars); } }
|
Reference
2734. Lexicographically Smallest String After Substring Operation