# 3. Longest Substring Without Repeating Characters

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mbob1UteEgulkLSHPJT%2F-MbossRPioI-w8mfws9i%2Fimage.png?alt=media\&token=6976679e-86da-46bd-a96f-2d25353de775)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mbob1UteEgulkLSHPJT%2F-Mbosvrfph9_J1H6FX1N%2Fimage.png?alt=media\&token=5bac17e5-f021-4019-aeb6-9c0badaeca74)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mbob1UteEgulkLSHPJT%2F-MbosocoBDtetLx-uHH-%2Fimage.png?alt=media\&token=ceca3dd4-db38-4824-a7e0-385d37ab310e)

time: O(n)

space: O(n)

```java
class Solution {
    public int lengthOfLongestSubstring(String s) {
        // ecounter same, for loop i, j index = map.get(same) + 1
        if (s == null || s.length() == 0) return 0;
        
        int res = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0, j = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                j = Math.max(j, map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i), i);
            res = Math.max(res, i - j + 1);
        }
        return res;
        
    }
}

```
