(Beats 100% python) Why do you ask me to do unnecessary things?
IntuitionDefinitely a medium problem. Had to rack my brain for how to represent the cyclic distance efficiently, without using something like a circular buffer
# Intuition
Definitely a medium problem. Had to rack my brain for how to represent the cyclic distance efficiently, without using something like a circular buffer of length 26.

Most importantly, there is always a '$$center$$' character that actually changes to anything other than an 'a'. If I could figure out what and where this character went, I thought could pay off in elegance and compute speed.

# Approach
Again, I was looking for elegance, not speed, and spend more time to drive down complexity on this problem.

And this approach paid off, as this apparently is the best performing solution according to the meter at the top of the page? ("beats 100%", I presume thats what this means).

That being said, the most interesting part about this problem was the cyclic distance function it asks you to make.

I ended up making a really elegant pair of functions to solve this, but it ended up being almost completely irellevant to the actual problem's solution through the approach I used. Regardless, I've included these in this post.

The solution simply requires you to loop through the string of characters, and only continue while 'k' is high enough to pay the distance cost. You can assume that for every iteration, there should be an 'a' at the beginning the the return string.

Most difficult was figuring out the precise logic for the 'middle' character. But its as simple as subtracting the remaining 'k'.

The edge cases were annoying too, but these were easily resolved using the "for... else" and returning all "a"'s under certain conditions.


# Complexity
- Time complexity: $$O(N)$$
- Space complexity: $$O(N)$$ (? unless you count the output string)

# Code
```python []
def chdist((c1,c2)):
    distance = ord(c1) - ord(c2)
    if distance > 13:
        distance = 26-distance
    return distance

def distance(s1, s2):
    return map(chdist, zip(s1,s2))

class Solution(object):
    def getSmallestString(self, s, k):
        i = 0
        dist = 0
        sign = None
        for i in range(len(s)):
            dist = ord(s[i]) - 97
            if dist > 13:
                dist = 26 - dist
            if k - dist <= 0:
                break
            else:
                k -= dist
        else:
            return "a" * len(s)
        
        if k >= dist:
            char = "a"
        else:
            char = chr(ord(s[i]) - k) 

        return ("a" * i) + char + s[i+1:]
        
```
['Python']