Correct Solution but time limit exceeds


  • 0
    K

    The solution I wrote exceeds the time limit. Is there a way that I can improve it, probably introduce Dynamic Programing?

    void countDistinctSubSeq(char* s, char* t, int* num){
        if(*t == '\0'){
            *num += 1;
            return;
        }
        
        while(*s != '\0' && *t != '\0'){
            if(*s == *t){
                countDistinctSubSeq(s+1, t+1, num);
            }
            ++s;
        }
    }
    
    int numDistinct(char* s, char* t) {
        int num = 0;
        countDistinctSubSeq(s, t, &num);
        return num;
    }
    

Log in to reply
 

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