55-The first non-repeating character in a character stream

Source: Internet
Author: User

When you read only the first two characters "go" from the character stream, the first character that appears only once is ' G '. When the first six characters "Google" are read out of the stream, a character that appears only 1 times is "L".

The first is to record the number of occurrences of a character , in order to achieve O (1) lookup, using simple hash table storage. Use occurences[256] to record the number of occurrences of a character. Set up:
Occurences[i] = 0, the character does not appear;
Occurences[i] = 1, the character appears once;
Occurences[i] = 2, the character appears 2 times or greater than 2 times
The character that appears once, using the FIFO queue record. In the subsequent character input process, you may enter a character that already exists once, there may be more than one occurrence of the character in the queue, so when you take the top element of the queue, you should check again if the element appears 1 times, if not, the queue pops until it finds a character index that appears only once.

time Complexity O (1),
Spatial complexity: occurences[256] space is constant; the queue will only have a maximum of 256 characters (only the first occurrence of the character). So space complexity O (1)

#include <iostream>#include <queue>using namespace STD;classCharstatics {Private:unsigned intoccurences[ the];intIndex Queue<int>Index_queue; Public: Charstatics () {index =-1; for(inti =0; I <=255; i++) Occurences[i] =0; }voidInsertchar (CharCH) {if(Occurences[ch] = =0) {Occurences[ch] =1;//First occurrence, set number of occurrences, press in queueIndex_queue.push (CH); }Else{Occurences[ch] =2;//2nd or more occurrences}     }CharFirstapperingonce () {//Find the character that appears first only once and use index to point to         while(!index_queue.empty () && Occurences[index_queue.front ()]! =1) {Index_queue.pop (); }if(!index_queue.empty ()) index = Index_queue.front ();Elseindex =-1;//No characters that appear only once        if(Index = =-1)return ' + ';returnindex+' + '; }};intMain () {charstatics str; Str. Insertchar (' G ');cout<< Str.    Firstapperingonce () << Endl; Str. Insertchar (' O ');cout<< Str.    Firstapperingonce () << Endl; Str. Insertchar (' O ');cout<< Str.    Firstapperingonce () << Endl; Str. Insertchar (' G ');cout<< Str.    Firstapperingonce () << Endl; Str. Insertchar (' l ');cout<< Str.    Firstapperingonce () << Endl; Str. Insertchar (' E ');cout<< Str. Firstapperingonce () << Endl;}

Output:

gggNULll

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

55-The first non-repeating character in a character stream

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.