A first offer to prove safety of the character stream without repeating characters

Title Description

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 only occurs once a "g". When reading out the first six characters "google" from the character stream, first appears only one character is "l".

Thinking

Hash: Create a number of occurrences of each character array representation, one by one read character after character corresponding to the array of numbers plus one, after traversing the string, if the number of occurrences of that character count is 1, then returns

Code

class Solution
{
public:
    string str;
    int Hash[128];
  //Insert one char from stringstream
    void Insert(char ch)
    {
        str = str + ch;
        Hash[ch-NULL]++;
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce()
    {
        if(str.size() == 0)
            return '#';
        for(int i = 0; i < str.size();i++)
        {
            if(Hash[str[i]-NULL] == 1)
                return str[i];
        }
        return '#';
    }
};
Published 85 original articles · won praise 0 · Views 403

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/104766160