简体   繁体   中英

C++: First non-repeating character, O(n) time using hash map

I'm trying to write a function to get the first non-repeating character of a string. I haven't found a satisfactory answer on how to do this in O(n) time for all cases. My current solution is:

char getFirstNonRepeated(char * str) {
    if (strlen(str) > 0) {
        int visitedArray[256] = {};    // Where 256 is the size of the alphabet
        for (int i = 0; i < strlen(str); i++) {
            visitedArray[str[i]] += 1;
        }

        for (int j = 0; j < 256; j++) {
            if (visitedArray[j] == 1) return j;
        }

    }
    return '\0';    // Either strlen == 0 or all characters are repeated
}

However, as long as n < 256, this algorithm runs in O(n^2) time in the worst case. I've read that using a hash table instead of an array to store the number of times each character is visited could get the algorithm to run consistently in O(n) time, because insertions, deletions, and searches on hash tables run in O(1) time. I haven't found a question that explains how to do this properly. I don't have very much experience using hash maps in C++ so any help would be appreciated.

Why are you repeating those calls to strlen() in every loop? That is linear with the length of the string, so your first loop effectively becomes O(n^2) for no good reason at all. Just calculate the length once and store it, or use str[i] as the end condition.

You should also be aware that if your compiler uses signed characters, any character value above 127 will be considered negative (and used as a negative, ie out of bounds, array offset). You can avoid this by explicitly casting your character values to be unsigned char.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM