Java - Compare left and right distances
Intuition\n Describe your first thoughts on how to solve this problem. \n- For each character, check if the left side distance or the right side distance is sma
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- For each character, check if the left side distance or the right side distance is smaller and update accordingly.\n- If you\'re able to move to character `\'a\'` in less than `k` steps, then directly set that character to `\'a\'` and decrement `distToA` from `k`.\n- Else, try to move your character left as much as possible.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n# Code\n```\nclass Solution {\n    public int cyclicDistance(char ch1, char ch2) {\n        int dist = Math.abs(ch1 - ch2);\n        return Math.min(dist, 26 - dist);\n    }\n    public String getSmallestString(String s, int k) {\n        char[] sArray = s.toCharArray();\n        for(int i = 0; i < sArray.length; i++) {\n            int distToA = cyclicDistance(sArray[i], \'a\');\n            if(distToA <= k) {\n                sArray[i] = \'a\';\n                k -= distToA;\n            } else if(k > 0) {\n                sArray[i] = (char) (sArray[i] -  k);\n                k = 0;\n            }\n        }\n        return new String(sArray);\n    }\n}\n```
['Java']