Simple solution O(n) counting
Intuitioniterate String and count all four directions.
calculate the Manhattan Distance if we change N to S and W to E or vise versa with k amount of value.Comp
# Intuition
iterate String and count all four directions.
calculate the Manhattan Distance if we change N to S and W to E or vise versa with k amount of value.

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(1)

# Code
```java []
class Solution {
    public int maxDistance(String s, int k) {
        int N = 0, S = 0, W = 0, E = 0;
        int n = s.length(), max = 0;
        for(int i=0;i<n;i++){
            if(s.charAt(i) == 'N') N++;
            else if(s.charAt(i) == 'S') S++;
            else if(s.charAt(i) == 'W') W++;
            else E++;
            max = Math.max(max, calc(N,S,W,E, k));
        }
        return max;
    }
// check Manhattan Distance with k change
    int calc(int N, int S, int W, int E, int k){
        int dist = 0;
        if(N > S){
            if(S<=k){
                N += S;
                k-=S;
                S = 0;
            }else{
                S -= k;
                N += k;
                k = 0;
            }
            dist += N - S;
        }else{
            if(N<=k){
                S += N;
                k-=N;
                N = 0;
            }else{
                N -= k;
                S += k;
                k = 0;
            }
            dist += S - N;
        }

        if(W > E){
            if(E<=k){
                W += E;
                k-=E;
                E = 0;
            }else{
                E -= k;
                W += k;
                k = 0;
            }
            dist += W - E;
        }else{
            if(W<=k){
                E += W;
                k-=W;
                W = 0;
            }else{
                W -= k;
                E += k;
                k = 0;
            }
            dist += E - W;
        }
        return dist;
    }
}
```
['Math', 'Counting', 'Java']