JavaScript Two pointer solution with explanation
Intuition\nTo make string lexicographically smaller we\'d need to replace as much letters to their precendents as possible. Letters at the beggining have higher
# Intuition\nTo make string lexicographically smaller we\'d need to replace as much letters to their precendents as possible. Letters at the beggining have higher weight comparing letters at the end of string.\n\n# Approach\nInitiate `left` pointer to 0 position. If `string[left]` is \'a\' move pointer to the right. Remember changing \'a\' to \'z\' makes string lexicographically bigger comparing to initial string.\n\nInitiate right pointer to the same value as left one. Move right pointer if (right + 1) is not \'a\' (remember avoid changing \'a\' to \'z\').\n\nThe final loop from `left` to `right` update current letter with precending letter.\n\n# Complexity\n- Time complexity:\nO(n) - we have 3 linear loops.\n\n- Space complexity:\nO(n) - to transform string into array.\n\n# Code\n```javascript []\n/**\n * @param {string} s\n * @return {string}\n */\nvar smallestString = function(s) {\n    const n = s.length;\n    let l = 0;\n    const arr = s.split(\'\');\n\n    while (l < (n-1) && arr[l] === \'a\') {\n        l++\n    }\n    let r = l;\n    while ((r+1) < n && arr[r+1] !== \'a\') {\n        r++\n    }\n\n    for (let i = l; i <= r && i < n; i++) {\n        if (arr[i] !== \'a\') {\n            arr[i] = String.fromCharCode(s.charCodeAt(i) - 1)\n        } else {\n            arr[i] = \'z\'\n        }\n    }\n\n    return arr.join(\'\');\n};\n```
['JavaScript']