题目描述
LeetCode 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, 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.
题目即要求找到一个字符串中不含重复字符的最长子串。子串(substring)必须是连续的,子序列(subsequence)才可以是不连续的。
算法分析
暴力解法当然是以每一个字符为起始位置,往后直到遇到重复字符为止。这样时间复杂度为O(n^2)的解法显然是不能通过的。