Brute Force Approach | C++
Complexity
Time complexity: O(N)

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

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

# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
        int ch = k;
        int x = 0;
        int y = 0;

        int result = INT_MIN;

        //North And West

        for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'N')
                y += 1;
            else if (s[i] == 'S') {
                if (ch > 0) {
                    y += 1;
                    ch -= 1;
                } else {
                    y -= 1;
                }
            }

            if (s[i] == 'W')
                x -= 1;
            else if (s[i] == 'E') {
                if (ch > 0) {
                    x -= 1;
                    ch -= 1;
                } else {
                    x += 1;
                }
            }
            result = max(result, (abs(x) + abs(y)));
        }
        x = 0;
        y = 0;
        ch = k;

        //South and West

        for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'S')
                y += 1;
            else if (s[i] == 'N') {
                if (ch > 0) {
                    y += 1;
                    ch -= 1;
                } else {
                    y -= 1;
                }
            }

            if (s[i] == 'W')
                x -= 1;
            else if (s[i] == 'E') {
                if (ch > 0) {
                    x -= 1;
                    ch -= 1;
                } else {
                    x += 1;
                }
            }
            result = max(result, (abs(x) + abs(y)));
        }
        x = 0;
        y = 0;
        ch = k;

        //North and East

        for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'N')
                y += 1;
            else if (s[i] == 'S') {
                if (ch > 0) {
                    y += 1;
                    ch -= 1;
                } else {
                    y -= 1;
                }
            }

            if (s[i] == 'E')
                x -= 1;
            else if (s[i] == 'W') {
                if (ch > 0) {
                    x -= 1;
                    ch -= 1;
                } else {
                    x += 1;
                }
            }
            result = max(result, (abs(x) + abs(y)));
        }
        x = 0;
        y = 0;
        ch = k;

        //South and East

        for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'S')
                y += 1;
            else if (s[i] == 'N') {
                if (ch > 0) {
                    y += 1;
                    ch -= 1;
                } else {
                    y -= 1;
                }
            }

            if (s[i] == 'E')
                x -= 1;
            else if (s[i] == 'W') {
                if (ch > 0) {
                    x -= 1;
                    ch -= 1;
                } else {
                    x += 1;
                }
            }
            result = max(result, (abs(x) + abs(y)));
        }

        return result;
    }
};
```
['C++']