C++ Simple Greedy
Couldn't solve this one during the contest, but found the obvious solution after some thought. In reality, there are only four ways which are going to give us t
Couldn't solve this one during the contest, but found the obvious solution after some thought. In reality, there are only four ways which are going to give us the optimal solution (Analyzing top right, top left, bottom right, and bottom left). We can simply iterate 4 times and greedily swap characters to meet the direction we want to go. This will give us our res.

# Code
```cpp []
class Solution {
public:
    int maxDistance(string s, int k) {
        int init = k;
        int n = 0;
        int s2 = 0;
        int w = 0;
        int e = 0;

        int res = 0;

        for (int i = 0;i<s.size();i++) {
            if (s[i]=='S'&&k>0) {
                k--;
                n++;
            } else if (s[i]=='S'){
                s2++;
            } else if (s[i]=='E'&&k>0) {
                w++;
                k--;
            } else if (s[i]=='E') {
                e++;
            } else if (s[i]=='N') {
                n++;
            } else if (s[i]=='W') {
                w++;
            }

            res = max(res,abs(n-s2)+abs(w-e));
        }

        n = 0;
        s2 = 0;
        w = 0;
        e = 0;
        k = init;

        for (int i = 0;i<s.size();i++) {
            if (s[i]=='N'&&k>0) {
                k--;
                s2++;
            } else if (s[i]=='N'){
                n++;
            } else if (s[i]=='W'&&k>0) {
                e++;
                k--;
            } else if (s[i]=='W') {
                w++;
            } else if (s[i]=='S') {
                s2++;
            } else if (s[i]=='E') {
                e++;
            }

            res = max(res,abs(n-s2)+abs(w-e));
        }

        n = 0;
        s2 = 0;
        w = 0;
        e = 0;
        k = init;

        for (int i = 0;i<s.size();i++) {
            if (s[i]=='S'&&k>0) {
                k--;
                n++;
            } else if (s[i]=='S'){
                s2++;
            } else if (s[i]=='W'&&k>0) {
                e++;
                k--;
            } else if (s[i]=='W') {
                w++;
            } else if (s[i]=='N') {
                n++;
            } else if (s[i]=='E') {
                e++;
            }

            res = max(res,abs(n-s2)+abs(w-e));
        }

        n = 0;
        s2 = 0;
        w = 0;
        e = 0;
        k = init;

        for (int i = 0;i<s.size();i++) {
            if (s[i]=='N'&&k>0) {
                k--;
                s2++;
            } else if (s[i]=='N'){
                n++;
            } else if (s[i]=='E'&&k>0) {
                w++;
                k--;
            } else if (s[i]=='E') {
                e++;
            } else if (s[i]=='S') {
                s2++;
            } else if (s[i]=='W') {
                w++;
            }

            res = max(res,abs(n-s2)+abs(w-e));
        }

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