First Unique Character in a substring


  • 0
    S

    This is what I came up with. However, it does not pass the runtime test when given extremely huge strings. Any ideas?

    int firstUniqChar(char* s) {
        int foundchar = 0;
        int currloc = -1;
        for (int i=0; i<strlen(s); i++){
            foundchar = 0;
            for (int j=0; j<strlen(s); j++){
                if (s[i]==s[j] & i != j){
                    foundchar = 1;
                    break;
                }
            }
                if (foundchar == 0) {
                    currloc = i;
                    break;
            }
        }
        if ((foundchar == 0) & (strlen(s) != 0)){
            return currloc;   
        }
        else{
            return -1;
        }
    
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.