Max step that can be altered at each step | Map | C++
IntuitionMax distance computation is constant time operation so I can compute the max distance at each step.ApproachIterate the string and at each step find
How
# Intuition
Max distance computation is constant time operation so I can compute the max distance at each step.

# Approach
Iterate the string and at each step find
1. How many steps can be altered?
2. Max distance from origin

### How many steps can be altered?
It should be always less or equal to curr step count and max allowed value (k). Hence:
`max_step_can_aleterd = min(i+1, k)`
Where `i` is current index.
However at each step it's not benefical to alter more than smaller value
Say you moved 5 step in N and 2 step in S, So net vertical movement = |5-2| = 3
It's always better to alter the smaller one to increase net movement in one direction.
Hence:
`max_step_can_aleterd = min(i+1, k, verMin + horMin)`

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
int maxDistance(string s, int k) {
    map<char, int> cnt; 
    int ans = 0;
    for(int i=0; i<s.size(); i++) {
        cnt[s[i]]++;

        int ver = abs(cnt['S'] - cnt['N']);
        int hor = abs(cnt['E'] - cnt['W']);
        
        int verMin = min(cnt['S'], cnt['N']);
        int horMin = min(cnt['E'], cnt['W']);
        
        // max step that can be altered
        int maxx = min({i+1, k, verMin + horMin});
        ans = max(ans, hor + ver + 2*maxx);
    }
    
    return ans;
}
```
['Ordered Map', 'C++']