Find the first un-repeated character in a string

后端 未结 30 1336
猫巷女王i
猫巷女王i 2020-11-27 18:29

What is the quickest way to find the first character which only appears once in a string?

30条回答
  •  暖寄归人
    2020-11-27 19:19

    Here is another approach...we could have a array which will store the count and the index of the first occurrence of the character. After filling up the array we could jst traverse the array and find the MINIMUM index whose count is 1 then return str[index]

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    #define No_of_chars 256
    
    //store the count and the index where the char first appear
    typedef struct countarray
    {
        int count;
        int index;
    }countarray;
    
    //returns the count array
        countarray *getcountarray(char *str)
        {
            countarray *count;
            count=new countarray[No_of_chars];
            for(int i=0;i array[i].index)
                    result = array[i].index;
            }
            delete[] (array);
            return (str[result]);
        }
    
        int main()
        {
            char str[] = "geeksforgeeks";
            cout<<"First non repeating character is "<

提交回复
热议问题