3.Longest-Substring-Without-Repeating-Characters
3. Longest Substring Without Repeating Characters
题目地址
https://leetcode.com/problems/longest-substring-without-repeating-characters/
题目描述
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.代码
Approach 1: Sliding Window
Complexity Analysis
Time complexity :
O(2n)Space complexity :
O(min(m, n))
Approach 2: Sliding Window Optimized
map: Character => Index
conatians character => update i
用 int[128] 代替 map: Character => index
Approach 3: Brute Force
Intuition
Check all the substring one by one to see if it has no duplicate character.
Complexity
Time complexity: O(n^3)
Space complexity: O(min(n,m)), The size of the Set is upper bounded by the size of the string n and the size of the charset/alphabet m.
Last updated
Was this helpful?