Longest Substring Without Repeating Characters
Question
Given a string, find the length of the longest substring without repeating characters.
Example
For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.
For "bbbbb" the longest substring is "b", with the length of 1.
Challenge
O(n) time
Tags
String Two Pointers Hash Table
Related Problems
Medium Longest Substring with At Most K Distinct Characters
Analysis
同样可以用窗口类型Two Pointers来解决,从两重for循环中优化算法,外层指针依次遍历,内层指针根据条件决定是否需要前进。
这样一类问题要熟练掌握思路:
Solution
LeetCode official: HashMap + Two Pointers
Instead of using a set to tell if a character exists or not, we could define a mapping of the characters to its index. Then we can skip the characters immediately when we found a repeated character.
Complexity Analysis
Time complexity :O(n)O(n). Indexjjwill iteratenntimes.
Space complexity (HashMap) :O(min(m,n))O(min(m,n)). Same as the previous approach.
Reference
Last updated