Simplest Solution || BEATS 100% || 8 lines of code
CodeIntuitionMaximum possible answer for any given problem is simply n where n is the length of the string.This can be achieved when there are no complements in
# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
       int ans=0;
        int n=s.size();
        map<char,int> mp;
        for(int i=0;i<n;i++){
            mp[s[i]]++;
            int temp =min(i+1,abs(mp['N']-mp['S']) + abs(mp['E']-mp['W']) +2*k);
            ans=max(ans,temp);
        }
        return ans;
    }
};
```
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Maximum possible answer for any given problem is simply n where n is the length of the string.This can be achieved when there are no complements in the string i.e No `'N' and 'S'` in same string and No `'E' and 'W'` together.

```
Example :
"NNNNWWW" -> 7 //(4,-3) = 4 +3
"WWW" ->3  //(0,-3) = 3
```



# Approach
<!-- Describe your approach to solving the problem. -->
We can use this intuition to develop a solution for any `i`.
```
i represents a substring from index 0 -> i. 
```

For every `i` we calculate a answer minimising the complements and therefore maximizing the result.
`complements  = abs(Freq(N) -Freq(S)) + abs(Freq(E) -Freq(W));`
```
It makes sense to only convert all 
'N's to 'S's if(freq(S) >freq(N))
'E's to 'W's if(freq(W) >freq(E))
 and so on...
```
```
if(Freq(N) >Freq(S))
converting one 'S' to 'N' will not only increase a point by
reducing one 'S' but also increase a point by adding a 'N'
Therefore one operation can increase our answer by two points and 
utilising k operation can increase answer by at most 2*k points.
So complements = abs(Freq(N) -Freq(S)) + abs(Freq(E) -Freq(W)) + 2*k;
```
 **Note :** Our Answer can never exceed the length of the string. 
`complements = min(complemets,length(i));`
# 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)$$ -->



['Hash Table', 'Greedy', 'C++']