C++ 
Approachtry to keep NE / NW / SE / SW and change k times if needed trying all 4Complexity
Time complexity:
O(n)
Space complexity:
O(1)Code

# Approach
<!-- Describe your approach to solving the problem. -->
try to keep NE / NW / SE / SW and change k times if needed trying all 4

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

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

# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
        int ans = 0, curr = 0;
        int changes;
        // NE
        int p, q;

        changes = k;
        p = q = 0;
        for (auto x : s) {
            if (x == 'N')
                p++;
            else if (x == 'E')
                q++;

            else if (x == 'S') {
                if (changes > 0)
                    p++, changes--;
                else
                    p--;
            } else {
                if (changes > 0)
                    q++, changes--;
                else
                    q--;
            }

            ans = max(ans, abs(p) + abs(q));
        }

        // NW
        changes = k;
        p = q = 0;
        for (auto x : s) {
            if (x == 'N')
                p++;
            else if (x == 'W')
                q--;

            else if (x == 'S') {
                if (changes > 0)
                    p++, changes--;
                else
                    p--;
            } else {
                if (changes > 0)
                    q--, changes--;
                else
                    q++;
            }

            ans = max(ans, abs(p) + abs(q));
        }

        // SW
        changes = k;
        p = q = 0;
        for (auto x : s) {
            if (x == 'S')
                p--;
            else if (x == 'W')
                q--;

            else if (x == 'N') {
                if (changes > 0)
                    p--, changes--;
                else
                    p++;
            } else {
                if (changes > 0)
                    q--, changes--;
                else
                    q++;
            }

            ans = max(ans, abs(p) + abs(q));
        }

        // SE
        changes = k;
        p = q = 0;
        for (auto x : s) {
            if (x == 'S')
                p--;
            else if (x == 'E')
                q++;

            else if (x == 'N') {
                if (changes > 0)
                    p--, changes--;
                else
                    p++;
            } else {
                if (changes > 0)
                    q++, changes--;
                else
                    q--;
            }

            ans = max(ans, abs(p) + abs(q));
        }

        return ans;
    }
};
```
['Hash Table', 'Math', 'String', 'Counting', 'C++']