My Solution
Intuition
N and S will cancel each other and so as W and E.


Make an array from the string and keep N as +1, S as -1, W as +1 and E as -1.


Now at each index
# Intuition
- N and S will cancel each other and so as W and E.
1. Make an array from the string and keep N as +1, S as -1, W as +1 and E as -1.
2. Now at each index i find the maximum possible distance, calculated as:
   - Let pos is total possitive value till i and neg is total negative values till i . 
   - Now in K operation you can invert max k +1s to -1s or k +1s to -1s. 
   - The ans will be the max of maximum negative value you can achive and minum value.

3. Similarly do it once again by swithing the values of E and W. (Keep E as +1 and W as -1) 


# Complexity
- Time complexity:
$$O(n)$$

- Space complexity:
$$O(n)$$

# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
        int n = s.size();
        vector<int>a(n);
        for(int i=0;i<n;i++){
            if(s[i] =='N' || s[i] == 'W'){
                a[i] = 1;
            }else a[i] = -1;
        }
        int pos = 0, neg = 0, ans = 0;

        for(int i=0;i<n;i++){
            if(a[i]>0) pos += 1;
            else neg+=1;
            ans = max({ans,pos + (k>=neg ? abs(neg) : 2*k-neg), abs(neg) + (k>=pos ? pos : 2*k-pos)});
        }
        pos = 0, neg = 0;
        for(int i=0;i<n;i++){
            if(s[i] =='N' || s[i] == 'E'){
                a[i] = 1;
            }else a[i] = -1;
        }

        for(int i=0;i<n;i++){
            if(a[i]>0) pos += 1;
            else neg+=1;
            ans = max({ans,pos + (k>=neg ? abs(neg) : 2*k-neg), abs(neg) + (k>=pos ? pos : 2*k-pos)});
        }

        return ans;
    }
};
```
['C++']