Leetcode | | Longest Substring without repeating characters (O (n) algorithm) problem

Source: Internet
Author: User

Problem:

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.

Thinking:


(1) Looking for substrings, to do a traversal.

(2) Brute force method, check each substring, use hash table mapping idea. Open an array, subscript the ASCII code of the character, because there is no description of the type of string, can take some measures to compress the size of hash table. The worst time complexity of brute force is O (n*n)

(3) an O (n) algorithm was found. Good location

In fact, much of the work here is repetitive and useless.
Look at the example:
S= "Abbdeca".
T1= "Abbdeca", t1[1]==t1[2].
T2= "Bbdeca". T2[0]==T2[1].


T3= "Bdeca", has been scanned to the last.
t4= "Deca", T5, T6, T7 all ibid.
We have scanned the s[2] while processing the T1, and then scanned s[2] to s[6 when processing T3, and the two substrings have scanned the entire string.
In other words, there are only two places where the substring can stop scanning: 1.s[2]. 2.S[6] (end).


For a sample s= "Aaab", the location where the substring stops scanning is: s[1],s[2],s[3] (end).


So we can consider just scanning the parent string, and taking the longest non-recurrent substring directly from the parent string.
For S[i]:
1.s[i] did not appear in the current substring. Then the length of the substring plus 1;
2.s[i] appears in the current substring, where the subscript is J. The starting position of the new substring must be greater than J, in order to make the new substring as long as possible, so the starting position is selected as j+1.

Code

Description: The solution of gaze complexity is O (n*n), bit O (n) without gaze

#include <iostream> #include <string> #include <memory.h>using namespace Std;/*class solution {public        : int lengthoflongestsubstring (string s) {int a[100];//Compressed hash table memset (a,0,sizeof (int) *100);        int length = S.size ();        int index=0;        int max=0;            for (int i=0;i<length;i++) {int j=i;                while ((j<length) && (a[s.at (j) -32]==0)//Compression hash Table {a[s.at (j) -32]=1;                index++;            j + +;            } memset (a,0,sizeof (int) *100);            max= (Max>index) Max:index;            index=0; if (j==length-1)//There is also a little trick here.        Can effectively reduce the time complexity of break;    } return max;        }};*/class Solution {Public:int lengthoflongestsubstring (string s) {//Start typing your C + + solution below        Do not write int main () function int locs[256];//holds the position of the last occurrence of the character memset (locs,-1, sizeof (locs)); int IDx =-1, max = 0;//idx is the start position of the current substring -1 for (int i = 0; i < s.size (); i++) {if (Locs[s[i]] > IDX            //Assuming that the current character has occurred, the starting position of the current substring is the position where the character last appeared +1 {idx = locs[s[i]]; } if (I-idx > Max)///Here is the key!

!!!!!

!! {max = I-idx; } Locs[s[i]] = i; } return max; }};int Main () {string str = "Abcdab"; Solution MySolution; Cout<<mysolution.lengthoflongestsubstring (str) <<endl;}



Leetcode | | Longest Substring without repeating characters (O (n) algorithm) problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.