Gavinmaxxing
IntuitionLowkey just like count. Every time you count, calcualte the best manhattan distance. North and South match, and South and West match. Find the manhatta
![1.png](https://assets.leetcode.com/users/images/99250173-4237-45a6-9e18-aa83c02a44ec_1738484203.2579756.png)

# Intuition
Lowkey just like count. Every time you count, calcualte the best manhattan distance. North and South match, and South and West match. Find the manhattan distance before any rewriting. Add the amount of k's you can add. The total K is the sum of the times North and South appear together and West and East. 

Example: 

NSS -> N appears one time, S appears two times. So then you can only pair once. 

NNNNNSSS -> N appears 5 times, S appears 3 times. So K can be use at most 3 times.




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

# Complexity
- Time complexity:
Linear

- Space complexity:
Constant

# Code
```cpp []
class Solution {
public:
    int maxDistance(string st, int k) {
        //n,e,s,w
        vector<int> c={0,0,0,0};
        int ans = 0, pair = 0;
        for ( auto it : st){
            int v = -1;
            if( it == 'N'){ v=0;}
            else if( it == 'E'){ v=1; }
            else if( it == 'S'){ v=2; }
            else if( it == 'W'){ v=3; }
            c[v]++;
            int cur = abs(c[2]-c[0])+abs(c[3]-c[1]);
            cur += 2*min(k,min(c[2],c[0])+min(c[3],c[1]));
            ans = max(ans,cur);
            //n e s w
        }
        for(auto it : c) cout << it << endl;
        return ans;
    }
};
```
['C++']