llllmz

导航

844. 比较含退格的字符串c

bool backspaceCompare(char* s, char* t) {
    int ns=strlen(s),nt=strlen(t);
    int heads=0,headt=0,index=0;
    while(index<ns){
        if(s[index]!='#'){
            s[heads++]=s[index++];
        }else{
            heads--;
            if(heads<0) heads=0;
            index++;
        }
    }
    index=0;
    while(index<nt){
        if(t[index]!='#'){
            t[headt++]=t[index++];
        }else{
            headt--;
            if(headt<0) headt=0;
            index++;
        }
    }
    if(heads==headt && heads==0) return true;
    if(heads!=headt)   return false;
    for(int i=0;i<heads;i++){
        printf("%c %c ",s[i],t[i]);
        if(s[i]!=t[i]) return false;
    }
    return true;
}   

 

posted on 2024-03-18 17:12  神奇的萝卜丝  阅读(13)  评论(0)    收藏  举报