Greedy solution O(n) [C++/Java/Python/Javascript] ||  Beats 100% solutions 
Tracking Coordinates:
Maintain (x, y) coordinates starting from (0, 0).
Update coordinates based on the character in the string (E, W, N, S).
Calculate Manhatta

# Tracking Coordinates:

1. Maintain (x, y) coordinates starting from (0, 0).
2. Update coordinates based on the character in the string (E, W, N, S).
# Calculate Manhattan Distance:

1. Compute the current Manhattan distance:
currentDistance=∣x∣+∣y∣
# Max Distance Calculation:

1. Evaluate the maximum distance considering three factors:
2. The current Manhattan distance.
3. The total steps up to the current index (i + 1).
4. The potential maximum distance with additional 2 * k moves.
# Result Update:

Continuously update maxDistance with the maximum of these three values.

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(1)

# Code
```cpp []
class Solution {
public:
    int maxDistance(std::string s, int k) {
        int x = 0, y = 0;  // Coordinates to track position
        int maxDistance = 0;

        for (int i = 0; i < s.length(); i++) {
            // Update coordinates based on direction
            if (s[i] == 'E') {
                x++;
            } else if (s[i] == 'W') {
                x--;
            } else if (s[i] == 'N') {
                y++;
            } else if (s[i] == 'S') {
                y--;
            }

            // Calculate Manhattan distance
            int currentDistance = abs(x) + abs(y);
            
            // Calculate possible maximum distance
            maxDistance = std::max({maxDistance, currentDistance, std::min(i + 1, currentDistance + 2 * k)});
        }

        return maxDistance;
    }
};
```
```java []
class Solution {
    public int maxDistance(String s, int k) {
        int x = 0, y = 0;
        int dis = 0;

        for (int i = 0; i < s.length(); i++) {
            char dir = s.charAt(i);
            if (dir == 'E') {
                x++;
            } else if (dir == 'W') {
                x--;
            } else if (dir == 'N') {
                y++;
            } else {
                y--;
            }

            int newDis = Math.abs(x) + Math.abs(y);
            dis = Math.max(dis, Math.max(newDis, Math.min(i + 1, newDis + 2 * k)));
        }

        return dis;
    }
}

```
```python []
class Solution:
    def maxDistance(self, s: str, k: int) -> int:
        x, y = 0, 0
        dis = 0

        for i, dir in enumerate(s):
            if dir == 'E':
                x += 1
            elif dir == 'W':
                x -= 1
            elif dir == 'N':
                y += 1
            else:
                y -= 1

            new_dis = abs(x) + abs(y)
            dis = max(dis, max(new_dis, min(i + 1, new_dis + 2 * k)))

        return dis

```
```javascript []
class Solution {
    maxDistance(s, k) {
        let x = 0, y = 0;
        let dis = 0;

        for (let i = 0; i < s.length; i++) {
            let dir = s[i];

            if (dir === 'E') {
                x++;
            } else if (dir === 'W') {
                x--;
            } else if (dir === 'N') {
                y++;
            } else {
                y--;
            }

            let newDis = Math.abs(x) + Math.abs(y);
            dis = Math.max(dis, Math.max(newDis, Math.min(i + 1, newDis + 2 * k)));
        }

        return dis;
    }
}
```
# PLEASE UPVOTE IF LIKE THE SOLUTION, AS IT SHOWS HOW MANY PEOPLE GET HELPED FROM THIS SOLUTION

['Math', 'Dynamic Programming', 'Greedy', 'C++', 'Java', 'JavaScript']