---
description: 2598 almost the same
---

# 3. Longest Substring Without Repeating Characters



````java
```java
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int left = 0;
        int result = 0;
        Map<Character, Integer> freqMap = new HashMap<>();

        for (int right = 0; right < n; right++) {
            char rightChar = s.charAt(right);
            freqMap.put(rightChar, freqMap.getOrDefault(rightChar, 0)+1);

            while (left <= right && freqMap.get(rightChar) > 1) {
                char leftChar = s.charAt(left);
                freqMap.put(leftChar, freqMap.getOrDefault(leftChar, 0)-1);
                if (freqMap.get(leftChar) == 0) {
                    freqMap.remove(leftChar);
                }
                left++;
            }
            result = Math.max(result, right - left + 1);
        }
        return result;
    }
}

/*
abcabc
   r
 l  
*/
```
````
