Contest 435 || Ques-2
Approach & IntuitionThe problem is likely about finding the maximum distance that can be traveled while making at most k changes to substitute one type of chara
### **Approach & Intuition**
The problem is likely about finding the maximum distance that can be traveled while making at most `k` changes to substitute one type of character for another. The solution explores all possible movement directions by considering four combinations of characters:  
- `('W', 'S')`
- `('E', 'S')`
- `('E', 'N')`
- `('W', 'N')`

For each combination, it calls the helper function to determine the maximum distance that can be achieved while making at most `k` modifications.

#### **`helper(String s, int k, char a, char b)` Explanation**
- It keeps track of the **current distance (`curr`)** and **maximum distance (`answer`)**.
- It iterates through the string:
  - If the current character matches either `a` or `b`, it increases `curr` (valid movement).
  - If the character is different and `k > 0`, it increments `curr` while consuming one modification (`k--`).
  - If no modifications are left, it decreases `curr`.
- The maximum value of `curr` encountered is stored in `answer`.

The final result is the **maximum** value among all four directional checks.

---

### **Time Complexity Analysis**
Let **n** be the length of the string `t`.

1. The function `helper` runs in **O(n)** time, iterating over the string once.
2. The function `maxDistance` calls `helper` **4 times**, leading to **O(4n) = O(n)** complexity.

Thus, the overall time complexity is **O(n)**.

---

### **Space Complexity Analysis**
- The solution only uses a few integer variables (`curr`, `answer`, `k`), leading to **O(1)** space usage.

Thus, the overall space complexity is **O(1)**.

---

### **Key Insights**
- The approach is similar to a **"sliding window with at most k modifications"** technique.
- Instead of keeping an explicit window, it maintains a running count (`curr`) and adjusts it dynamically.
- The function tries all four possible directional movement combinations and takes the maximum.

This approach is **efficient** and works well within constraints. 🚀

# Code
```java []
class Solution {
    public int maxDistance(String t, int k) {
       
       return Math.max(helper(t,k,'W','S'),Math.max(helper(t,k,'E','S'),Math.max(helper(t,k,'E','N'),helper(t,k,'W','N'))));
    }
    int helper(String s , int k , char a , char b){
        int curr=0;
        int answer=0;
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if(c==a || c==b) curr++;
            else if(k>0){
                curr++;
                k--;
            }
            else curr--;
            answer = Math.max(answer,curr);
        }
        return answer;
    }
}
```
['Java']