Longest Substring w /wo xxx characters

leetcode 3 : Longest Substring w/o Repeating Characters.

Given a string, find the length of the longest substring without repeating characters.

Eg. Given abcabcbb, result is 3 (abc); Given bbbbb, result is 1 (b); Given pwwkew, returns 3 (wke; note that it is substring that we are working on , not subsequence (such as "pwke").

使用哈希表和双指针/下标(分别指向substring的首尾);尾指针向前遍历字符串,凡是没遇到过的字符就加入哈希表;如若遇到重复字符,则移动首指针到此字符上一次出现的位置之后,并更新此字符的哈希值。

后续升级问题:

Leetcode 159: Longest Substring with At Most two distinct characters.

Given a string S, find the length of the longest substring T that contains at most two distinct characters.
Eg. Given “eceba”, result is 3(ece)

&

Leetcode 340: Longest Substring with At Most K distinct characters.

可参考:www.cnblogs.com/grandyang/p/5351347.html

posted @ 2017-05-02 14:48  Calvmy  阅读(759)  评论(0)    收藏  举报