Moving Along Diagonals North-East,North-west, South-East, South-West
IntuitionMximum Mahattan Distance is when we move across north _east,north_west, south_east, south_westApproachComplexity
Time complexity:

Space complexity:
Co
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Mximum Mahattan Distance is when we move across north _east,north_west, south_east, south_west
# Approach
<!-- Describe your approach to solving the problem. -->

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

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

# Code
```java []
class Solution {
   public int north_east(String s, int k) {
    StringBuilder sb = new StringBuilder("");
    int i = 0;
    while (i < s.length() && k > 0) {
        if (s.charAt(i) == 'S') {
            sb.append('N');
            k--;
        } else if (s.charAt(i) == 'W') {
            sb.append('E');
            k--;
        } else {
            sb.append(s.charAt(i));
        }
        i++;
    }
    sb.append(s.substring(i));
    int total = 0;
    int maxTotal = Integer.MIN_VALUE;
    for (char ch : sb.toString().toCharArray()) {
        if (ch == 'N' || ch == 'E') total++;
        else total--;
        maxTotal = Math.max(maxTotal, total);
    }
    return maxTotal;
}

public int north_west(String s, int k) {
    StringBuilder sb = new StringBuilder("");
    int i = 0;
    while (i < s.length() && k > 0) {
        if (s.charAt(i) == 'S') {
            sb.append('N');
            k--;
        } else if (s.charAt(i) == 'E') {
            sb.append('W');
            k--;
        } else {
            sb.append(s.charAt(i));
        }
        i++;
    }
    sb.append(s.substring(i));
    int total = 0;
    int maxTotal = Integer.MIN_VALUE;
    for (char ch : sb.toString().toCharArray()) {
        if (ch == 'N' || ch == 'W') total++;
        else total--;
        maxTotal = Math.max(maxTotal, total);
    }
    return maxTotal;
}

public int south_east(String s, int k) {
    StringBuilder sb = new StringBuilder("");
    int i = 0;
    while (i < s.length() && k > 0) {
        if (s.charAt(i) == 'N') {
            sb.append('S');
            k--;
        } else if (s.charAt(i) == 'W') {
            sb.append('E');
            k--;
        } else {
            sb.append(s.charAt(i));
        }
        i++;
    }
    sb.append(s.substring(i));
    int total = 0;
    int maxTotal = Integer.MIN_VALUE;
    for (char ch : sb.toString().toCharArray()) {
        if (ch == 'S' || ch == 'E') total++;
        else total--;
        maxTotal = Math.max(maxTotal, total);
    }
    return maxTotal;
}

public int south_west(String s, int k) {
    StringBuilder sb = new StringBuilder("");
    int i = 0;
    while (i < s.length() && k > 0) {
        if (s.charAt(i) == 'N') {
            sb.append('S');
            k--;
        } else if (s.charAt(i) == 'E') {
            sb.append('W');
            k--;
        } else {
            sb.append(s.charAt(i));
        }
        i++;
    }
    sb.append(s.substring(i));
    int total = 0;
    int maxTotal = Integer.MIN_VALUE;
    for (char ch : sb.toString().toCharArray()) {
        if (ch == 'S' || ch == 'W') total++;
        else total--;
        maxTotal = Math.max(maxTotal, total);
    }
    return maxTotal;
}

    public int maxDistance(String s, int k) {
    int ne=north_east(s,k);
    int nw=north_west(s,k);
    int se=south_east(s,k);
    int sw=south_west(s,k);
    return Math.max(ne,Math.max(nw,Math.max(se,sw)));
    }
}
```
['Java']