Problem Statement
Given a string s, find the length of the longest substring without repeating characters.
Approach
Use sliding window with a hash set. Expand right pointer, shrink left when duplicate found.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(min(n, m)) where m is charset size |
Examples
Example 1
Input: s = "abcabcbb"
Output: 3
Explanation: Answer is "abc", with length 3
Example 2
Input: s = "bbbbb"
Output: 1
Explanation: Answer is "b", with length 1
Example 3
Input: s = "pwwkew"
Output: 3
Explanation: Answer is "wke", with length 3
Constraints
- ▸
0 <= s.length <= 5 * 10^4 - ▸
sconsists of English letters, digits, symbols and spaces
Loading...
Sign in to run your code...
Asked by companies:
AmazonFacebookBloombergMicrosoftApple