Easiest Solution, with comments
Intuitionyou have to assume x dir and y dir and than choose which dir to take dominat use all combination and fetch answerApproachComplexity
Time complexity:

S
# Intuition
you have to assume x dir and y dir and than choose which dir to take dominat use all combination and fetch answer

# 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
```cpp []
#include <bits/stdc++.h>
using namespace std;

int solve(int n, string &s, int k, char dir1, char dir2) {
    int x = 0, y = 0;
    int maxDist = 0;

    for (int i = 0; i < n; i++) {
        // If we're on N/S path
        if (s[i] == 'N' || s[i] == 'S') {
            if (dir1 != s[i]) {  
                if (k > 0) {
                    k--;
                    if (dir1 == 'N') {
                        y++;
                    } else {
                        y--;
                    }
                } else {
                    if (s[i] == 'N') {
                        y++;
                    } else {
                        y--;
                    }
                }
            } else {
                if (s[i] == 'N') {
                    y++;
                } else {
                    y--;
                }
            }
        }
        // If we're on E/W path
        else if (s[i] == 'E' || s[i] == 'W') {
            if (dir2 != s[i]) {  // Change direction if k allows
                if (k > 0) {
                    k--;
                    if (dir2 == 'E') {
                        x--;
                    } else {
                        x++;
                    }
                } else {
                    if (s[i] == 'E') {
                        x--;
                    } else {
                        x++;
                    }
                }
            } else {
                if (s[i] == 'E') {
                    x--;
                } else {
                    x++;
                }
            }
        }

        // Update max distance
        maxDist = max(maxDist, abs(x) + abs(y));
    }
    return maxDist;
}

class Solution {
public:
    int maxDistance(string s, int k) {
        int n = s.length();
        int maxDist = 0;
        
        // Check all 4 possible combinations of directions
        maxDist = max(maxDist, solve(n, s, k, 'N', 'E'));
        maxDist = max(maxDist, solve(n, s, k, 'N', 'W'));
        maxDist = max(maxDist, solve(n, s, k, 'S', 'E'));
        maxDist = max(maxDist, solve(n, s, k, 'S', 'W'));

        return maxDist;
    }
};

```
['C++']