[ Swift ] O(n) solution
Intuition\nLexicographical order of s string is determined by the letters from left to right.\n\nTherefore, we should prioritize making each letter in the strin
# Intuition\nLexicographical order of s string is determined by the letters from left to right.\n\nTherefore, we should prioritize making each letter in the string as lexographically small as possible, starting from the left.\n\n# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$, excluding the space used for the answer\n\n# Code\n```\nclass Solution {\n    func getSmallestString(_ s: String, _ k: Int) -> String {\n        guard k > 0 else {\n            return s\n        }\n        var ans: String = ""\n        var remK: Int = k\n        for c in s {\n            if remK > 0 {\n                let newC = lowestCharWithinK(c, remK)\n                remK -= distanceBetween(newC, c)\n                ans.append(newC)\n            } else {\n                ans.append(c)\n            }\n        }\n        return ans\n    }\n    \n    func charToInt(_ c: Character) -> Int {\n        return Int(exactly: c.asciiValue!)! - 97\n    }\n    \n    func intToChar(_ n: Int) -> Character {\n        return Character(UnicodeScalar(n + 97)!) \n    }\n    \n    // Calculate cyclic distance between 2 chars\n    func distanceBetween(_ a: Character, _ b: Character) -> Int {\n        if a == b {\n            return 0\n        } else {\n            var diff: Int = abs(charToInt(a) - charToInt(b))\n            return min(diff, 26 - diff)\n        }\n    }\n    \n    // Find the lexographically smallest char within k distance of c\n    // Either "a" or k letters before c\n    func lowestCharWithinK(_ c: Character, _ k: Int) -> Character {\n        let c: Int = charToInt(c)\n        if c - k > 0 && c + k < 26 {\n            return intToChar(c - k)\n        }\n        return intToChar(0)\n    }\n}\n```
['Greedy', 'Swift']