---
# System prepended metadata

title: Leetcode 3. Longest Substring Without Repeating Characters
tags: [Leetcode(C++)]

---

# Leetcode 3. Longest Substring Without Repeating Characters
###### tags: `Leetcode(C++)`

題目 : https://leetcode.com/problems/longest-substring-without-repeating-characters/ 。

想法 : 滑動視窗。

時間複雜度 : O(n)。

程式碼 :

```
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int l=s.size(),lead=0,tail=0,alp[270]={0},ans=0;
        
        while(lead<l){
            int now=s[lead];
            while(alp[now] == 1){ 
                int where=s[tail];
                alp[where]--;
                tail++;   
            }
            
            alp[now]++;
            lead++;
            ans=max(ans,lead-tail);
        }
        
        return ans;
    }
};
```