Java solution 
Intuition
The problem requires finding the maximum Manhattan distance from the origin while allowing up to k modifications in movement directions.
Since each mo
# Intuition
* The problem requires finding the maximum Manhattan distance from the origin while allowing up to `k` modifications in movement directions.
* Since each movement affects either the x or y coordinate, we can track the count of each movement ('N', 'S', 'E', 'W') and adjust them optimally using `k`.
* The goal is to maximize the absolute difference in both horizontal (`x`) and vertical (`y`) movements.

# Approach
1. **Count Movements**  
   * Traverse the string `s` and count occurrences of each direction:
     * `n` → count of 'N'
     * `s` → count of 'S'
     * `e` → count of 'E'
     * `w` → count of 'W'

2. **Determine Effective Movements**  
   * Compute the stronger and weaker movements:
     * `max1 = max(n, s)`, `min1 = min(n, s)` (Vertical movements)
     * `max2 = max(e, w)`, `min2 = min(e, w)` (Horizontal movements)

3. **Optimize Using `k` Modifications**  
   * The total weaker movements (`min1 + min2`) can be changed up to `k` times to maximize distance.
   * Compute the modified distance:
     ```
     distance = (max1 + max2) + min(min1 + min2, k) - ((min1 + min2) - min(min1 + min2, k))
     ```
   * Update `max` with the maximum distance encountered.

4. **Return Maximum Distance**  
   * After iterating through the string, return the highest computed Manhattan distance.

# Complexity
- **Time complexity:**  
  * `O(n)`, since we iterate through the string once.  

- **Space complexity:**  
  * `O(1)`, as we only use a few integer variables for counting.


# Code
```java []
class Solution {
    public int maxDistance(String str, int k) {
        int max=0;
        int s=0;
        int n =0;
        int e=0;
        int w=0;
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            if(ch=='S'){
                s++;
            }
            else if(ch=='N'){
                n++;
            }
            else if(ch=='E'){
                e++;
            }
            else{
                w++;
            }
            int max1=Math.max(s , n);
            int min1=Math.min(s,n);
            int max2=Math.max(e,w);
            int min2=Math.min(e,w);
            int temp=(max1+max2)+Math.min((min1+min2),k) -((min1+min2)- Math.min((min1+min2),k));
            max=Math.max(max, temp);
        }
        return max;
    }
}
```
['Java']