 One pass Without checking 4 direction || check max for each i || Greedy
IntuitionAssume each index as the end indexApproachfor each index assume it to be the last index of best sequence with k changes
At dis we multipy 2 to ensure i
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Assume each index as the end index
# Approach
<!-- Describe your approach to solving the problem. -->
for each index assume it to be the last index of best sequence with k changes
* At dis we multipy 2 to ensure it is add the distance which get reduced in the finding absolute distance X and Y.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
      int ans = 0,north = 0,south=0,east=0,west=0;
        for(int i=0;i<s.size();i++){
            if(s[i] == 'N')north++;
            if(s[i] == 'S')south++;
            if(s[i] == 'E')east++;
            if(s[i] == 'W')west++;
            int x = abs(north-south);
            int y = abs(east-west);
            int ini = x+y;
            int dis = ini + min(2*k,i+1-ini); // ensure we change dir such that change will be atmost k;
            ans = max(ans,dis);
        }
        return ans;
    }
};
```
['Greedy', 'C++']