[Python3] math
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def maxDistance(self, s: str, k: int) -> int:
        ans = 0 
        freq = Counter()
        for ch in s: 
            freq[ch] += 1
            v = min(freq['N'], freq['S'])
            h = min(freq['E'], freq['W'])
            if v + h <= k: cand = sum(freq.values())
            elif v <= k: cand = freq['N'] + freq['S'] + max(freq['E'], freq['W']) + 2*(k-v) - h
            else: cand = max(freq['N'], freq['S']) + 2*k - v + abs(freq['E'] - freq['W'])
            ans = max(ans, cand)
        return ans 
```
['Python3']