Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.
Using dynamic solver: For a known string s[1, ..., K], use a hash table table[256] record s[i] where it appears, starting with-1.
Assuming that the longest non-repeating length of a substring with the suffix s[k] is known to be suffix_max[k], then for S[1, ..., k+1],
A) if table[s[k+1]] is-1, indicating s[k+1] first appearance, suffix_max[k+1] = suffix_max[k] + 1;
b) Otherwise, use Last_visit record s[k+1] Last occurrence position, if Last_visit <= Table[s[i]],
1 intLengthoflongestsubstring (strings)2 {3 inttable[ the];4 intSuffix_max =0;//Suffix_max contains the maximum length of the suffix5 intGlobal_max =0;6 intLast_visit =0;//last time7 8memset (table,-1, the*sizeof(int));9 for(inti =0; I < s.size (); i++)Ten { One if(Table[s[i]] = =-1)//Not appear before A { -suffix_max++; - } the Else - { - if(Last_visit <=Table[s[i]]) - { +Suffix_max = i-Table[s[i]]; -Last_visit = Table[s[i]] +1; + } A Else at { -suffix_max++; - } - } -Hash[s[i]] =i; - if(Global_max <Suffix_max) inGlobal_max =Suffix_max; - } to + returnGlobal_max; -}
Leetcode 3.Longest Substring without repeating characters