O(n) time O(1) space || c++
Code
# Code
```cpp []
class Solution {
public:
    int maxDistance(string t, int k) {
        int e = 0, w = 0, n = 0, s = 0;
        int m = t.size(); 
        int res = 0;
        for(int i = 0; i < m; i++) {
            
            if(t[i] == 'N') {
                n++;
            } else if(t[i] == 'S') {
                s++; 
            } else if(t[i] == 'E') {
                e++;
            } else {
                w++; 
            }
            
            // lets suppose this is the peak
            
            // case 1: NE 
            res = max(res, n - s + e - w + 2*min(k, s+w)); 
            
            // case 2: NW 
            res = max(res, n - s + w - e + 2*min(k, s+e)); 
            
            // case 3: SE 
            res = max(res, s - n + e - w + 2*min(k, n+w)); 
            
            // case 4: SW 
            res = max(res, s - n + w - e + 2*min(k, n+e));
        }
        return res;
    }
};
```
['C++']