C++ | Greedy | O(n)
Code
# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
        int maxN = 0, maxS = 0, maxE = 0, maxW = 0, ans = 0;
        for(auto &p : s){
            if(p == 'N') maxN++;
            if(p == 'S') maxS++;
            if(p == 'W') maxW++;
            if(p == 'E') maxE++;
            int maxY = max(maxN, maxS);
            int maxX = max(maxW, maxE);
            int minY = min(maxN, maxS);
            int minX = min(maxW, maxE);
            int curr = maxX - minX + maxY - minY;
            int tempK = k;
            int newMinX = max(0, minX - k);
            k -= (minX - newMinX);
            int newMinY = max(0, minY - k);
            k -= (minY - newMinY);
            int usedK = tempK - k;
            curr = maxX - newMinX + maxY - newMinY + usedK;
            ans = max(ans, curr);
            k = tempK;
        }
        return ans;
    }
};
```
['C++']