Just go with the flow. O(n) time
ApproachJust go with the flow and check if current manhatten distance at position i is less than max possible of i+1, if so, increase the dist by 2 * k limited


# Approach
Just go with the flow and check if current manhatten distance at position *i* is less than max possible of *i+1*, if so, increase the dist by *2 * k* limited to current max distance possible.
As the manhatten distance would be less than *i+1* only if we walk back in back direction, so we would reverse that and add 1 in the same direction to achieve maximum manhatten distance or add *2 * k* at max.
# Complexity
- Time complexity:
$$O(n)$$

- Space complexity:
$$O(1)$$

# Code
```java []
class Solution {
    public int maxDistance(String s, int k) {
        int ans = 0, x = 0, y = 0;
        for(int i = 0; i < s.length(); i++) {
            switch(s.charAt(i)) {
                case 'N' -> y++;
                case 'S' -> y--;
                case 'W' -> x++;
                default  -> x--;
            }
            int dist = Math.abs(x) + Math.abs(y);
            if(dist < i + 1) {
                dist = Math.min(i + 1, dist + 2 * k);
            }
            ans = Math.max(ans, dist);
        }
        return ans;
    }
}
```
['Java']