Title: Oldest string with no repeating characters (C + +)

Source: Internet
Author: User
Tags repetition

The first thing I think about when I get this question is to use the map to count the occurrences of the characters in the string, and then record the oldest string to write the program as follows:

Static ConstAuto IO_SPEED_UP =[] () {Std::ios::sync_with_stdio (false);    Cin.tie (nullptr); return 0;} ();classSolution { Public:    intLengthoflongestsubstring (strings) {CharC; intBegin =0; intMaxLength =0; intNowlength =0; size_t length=s.length (); Unordered_map<Char,int>strmap{{'a', -1}, {'b', -1}, {'C', -1}, {'D', -1}, {'e', -1}, {'F', -1}, {'g', -1}, {'h', -1},            {'I', -1}, {'J', -1}, {'k', -1}, {'L', -1}, {'m', -1}, {'N', -1}, {'o', -1}, {'P', -1},            {'Q', -1}, {'R', -1}, {'s', -1}, {'T', -1}, {'u', -1}, {'v', -1}, {'W', -1}, {'x', -1},            {'y', -1}, {'Z', -1}        };  for(size_t i =0; i < length; ++i) {c=s.at (i); if(Strmap[c] = =-1)            {                ++nowlength; STRMAP[C]=i; }            Else{maxLength= maxLength > Nowlength?maxlength:nowlength; Nowlength-= (Strmap[c]-begin);  while(Begin <=Strmap[c]) {strmap[s.at (begin)]= -1; ++begin; } Strmap[c]=i; }        }        returnMaxLength > Nowlength?maxlength:nowlength; }};

But after the submission found only faster than 65.17% of the program, went to see the best solution, the best solution code is as follows:

Static ConstAuto IO_SPEED_UP =[] () {Std::ios::sync_with_stdio (false);    Cin.tie (nullptr); return 0;} ();classSolution { Public:    intLengthoflongestsubstring (strings) {intSize =s.size (); Vector<int> Dict ( the,-1); intMaxLen =0; intStart =-1;  for(intI=0; i<size;i++) {            if(Dict[s[i]) >start) {Start=Dict[s[i]]; } Dict[s[i]]=i; MaxLen= Max (MaxLen, I-start); }        returnMaxLen; }};

The two ideas are similar to the characters in the corresponding strings appear, when the repetition of characters, discard the occurrence of the repetition of the character before the position, the new substring start position set to its + one count substring length, and finally the longest substring length. Execution speed Difference I think it appears in the following two areas:

One is to create a vector array of length (up to 256 char) to compare characters, which can take advantage of data locality and speed up memory reading.

Second, his comparison is to compare whether the new characters appear farther than the previous character, and then replace them directly. And I'm comparing the occurrences of the character corresponding to whether the node is initialized to-1 if so, then replace, so that I have repeated characters in the case of the previous string to reset, there are more loops.

Sometimes you have to admit that someone else is writing a shorter program than you, easier to understand, and more efficient than you. Sad

Title: Oldest string with no repeating characters (C + +)

Related Article

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.