ACWING64. Character stream only once in the first character (to prove safety offer) appears

Please implement a function to find the character stream for the first time a character appears only.

For example, when the character stream reads only the first two characters "go", the first character appears only once is 'g'.

When reading out the first six characters "google" from the character stream, the first character appears only once is 'l'.

If the current character stream does not appear there is a character, returns the # character.

Sample
input: "google"

Output: "ggg # ll"

Explanation: each time the character stream reads a character, it is determined once and outputs the current first character appears only once.

Ideas:
by force, plus a stack in the ok

class Solution{
public:
    //Insert one char from stringstream
    queue<char>Q;
    int vis[200] = {0};
    
    void insert(char ch){
        vis[ch]++;
        if(Q.empty() || vis[ch] == 1) {
            Q.push(ch);   
        }
        while(!Q.empty() && vis[Q.front()] > 1) Q.pop();
    }
    //return the first appearence once char in current stringstream
    char firstAppearingOnce(){
        if(Q.empty()) return '#';
        else return Q.front();
    }
};
Published 844 original articles · won praise 28 · views 40000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104973427