Given a string, find the length of the longest substring without repeating characters.spa
Examples:code
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.blog
Given "bbbbb"
, the answer is "b"
, with the length of 1.string
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.it
1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 vector<int> dict(256, -1); 5 int maxLen = 0, start = -1; 6 for (int i = 0; i != s.length(); i++) { 7 if (dict[s[i]] > start) 8 start = dict[s[i]]; 9 dict[s[i]] = i; 10 maxLen = max(maxLen, i - start); 11 } 12 return maxLen; 13 } 14 };