Best Solution for Arrays 🚀 in C++, Python and Java || 100% working
Intuition💡 To find the Longest Substring Without Repeating Characters, we use a sliding window approach. This method dynamically adjusts the window size to ensu
# Intuition  
💡 To find the **Longest Substring Without Repeating Characters**, we use a **sliding window** approach. This method dynamically adjusts the window size to ensure all characters inside the window are unique.  

# Approach  
👉 Use two pointers, `l` and `r`, to define a window over the string.  
👉 Maintain a frequency array or set to track characters in the current window.  
👉 Expand the window by moving `r`. If a duplicate is found, shrink the window by moving `l`.  
👉 Track the maximum window size during this process.

# Complexity  
- Time complexity: $$O(n)$$ 🔥  
- Space complexity: $$O(1)$$ (fixed size for ASCII or Unicode) ✨  

# Code  

```cpp []
Code
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ans = 0;
        vector<int> arr(128, 0);
        int l = 0;
        for (int r = 0; r < s.length(); r++) {
            int curr = s[r];
            while (arr[curr] > 0) {
                arr[s[l]]--;
                l++;
            }
            arr[curr]++;
            ans = max(ans, r - l + 1);
        }
        return ans;
    }
};
```

```python []
Code
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        ans = 0
        arr = [0] * 128
        l = 0
        for r in range(len(s)):
            curr = ord(s[r])
            while arr[curr] > 0:
                arr[ord(s[l])] -= 1
                l += 1
            arr[curr] += 1
            ans = max(ans, r - l + 1)
        return ans
```

```java []
Code
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int ans = 0;
        int[] arr = new int[128];
        int l = 0;
        for (int r = 0; r < s.length(); r++) {
            int curr = s.charAt(r);
            while (arr[curr] > 0) {
                arr[s.charAt(l)]--;
                l++;
            }
            arr[curr]++;
            ans = Math.max(ans, r - l + 1);
        }
        return ans;
    }
}
```  
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['Hash Table', 'String', 'Sliding Window', 'Python', 'C++', 'Java', 'Python3']