Python Solution.  Tricky One
IntuitionI was not able to solve this question in contest.
Tried few method based on opposite direction(edge cases failed), backtracking(TLE) ended up no where.
# Intuition

I was not able to solve this question in contest. 
Tried few method based on opposite direction(edge cases failed), backtracking(TLE) ended up no where.

After the contest I went through the hint and came up with this logic

# Approach

Approach to this problem very tricky. 
- Traverse the given string quadrant wise
- Consider the char which are going away the quadrant.
- char which are going towards the quadrant; check if k>0 if yes add that char and reduce k by 1
- do it until traverse the string and store the max
- do it until ur traverse the string in all quadrants and compare the max
- return max


---



![image.png](https://assets.leetcode.com/users/images/6a3d2897-6335-4418-b96f-bf9ab3d6d862_1738483449.2701192.png)


# Code
```python []
class Solution(object):
    def maxDistance(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        ans = 0
        dir = [['N', 'E'], ['N', 'W'], ['S', 'E'], ['S', 'W']]
        for d in dir:
            curr = 0
            t = k
            for i in range(len(s)):
                if s[i] == d[0] or s[i] == d[1]:
                    if t > 0:
                        t -= 1
                        curr += 1
                    else:
                        curr -= 1
                else:
                    curr += 1
                ans = max(ans, curr)
        return ans
```

---


**UPVOTE PLEASE **
![image.png](https://assets.leetcode.com/users/images/62df8b87-cab1-452a-8ecf-3b8ab427414d_1738483506.789267.png)

['Array', 'Math', 'Python']