Diagonals | Greedy | O(n)
IntuitionApproachComplexity
Time complexity: O(n)

Space complexity: O(4)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

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

# Code
```java []
class Solution {
    public int maxDistance(String s, int k) {

        int n = s.length();
        int currDist[] = new int[4];
        int maxDist = 0;

        int k1 = k, k2 = k, k3 = k;

        for(int i = 0; i < n; i++) { 
            char ch = s.charAt(i);
            
            // top right
            if(ch == 'N' || ch == 'E') {
                currDist[0]++;
            } else {
                if(k-- > 0) {
                    currDist[0]++;
                } else {
                    currDist[0]--;
                }
            }

            // top left 
            if(ch == 'N' || ch == 'W') {
                currDist[1]++;
            } else {
                if(k1-- > 0) {
                    currDist[1]++;
                } else {
                    currDist[1]--;
                }
            }

            // bottom left
            if(ch == 'W' || ch == 'S') {
                currDist[2]++;
            } else {
                if(k2-- > 0) {
                    currDist[2]++;
                } else {
                    currDist[2]--;
                }
            }

            // bottom right
            if(ch == 'S' || ch == 'E') {
                currDist[3]++;
            } else {
                if(k3-- > 0) {
                    currDist[3]++;
                } else {
                    currDist[3]--;
                }
            }

            maxDist = Integer.max(maxDist, Integer.max(currDist[0], Integer.max(currDist[1], Integer.max(currDist[2], currDist[3]))));
        }

        return maxDist;
    }
}
```
['String', 'Greedy', 'Java']