Greedy, sliding window O(n) solution
IntuitionTake sliding window from the beginning and try to get maximum displacement in north-south and east-west directions.Complexity
Time complexity: O(n)
Cod
# Intuition
Take sliding window from the beginning and try to get maximum displacement in north-south and east-west directions.



# Complexity
- Time complexity: O(n)


# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
        int ans = INT_MIN;
        int n = s.size();
        int north = 0, south = 0, east = 0, west = 0;
        for(int i = 0; i < n; i++){
            int currAns = 0;
            int currK = k;
            if(s[i] == 'N')
            north++;
            else if(s[i] == 'S')
            south++;
            else if(s[i] == 'E')
            east++;
            else
            west++;

            //for north-south
            int bigger = max(north, south);
            int smaller = min(north, south);
            int add = min(currK, smaller);
            int subtract = smaller - add;
            currAns += bigger + add - subtract;

            //for east-west
            currK = currK - add;
            bigger = max(east, west);
            smaller = min(east, west);
            add = min(currK, smaller);
            subtract = smaller - add;
            currAns += bigger + add - subtract;

            ans = max(ans, currAns);


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