llllmz

导航

1312. 让字符串成为回文串的最少插入次数c

int min;

void dfs(char* s,int head,int tail, int count){
    if(head>=tail){
        if(count<min) min=count;
        return ;
    }
    if(s[head]==s[tail]){
        dfs(s,head+1,tail-1,count);
    }else{
        dfs(s,head+1,tail,count+1);
        dfs(s,head,tail-1,count+1);
    }
}

int minInsertions(char* s) {
    int n=strlen(s);
    if(n==1) return 0;
    min=INT_MAX;
    int head=0,tail=n-1,count=0;
    dfs(s,head,tail,count);
    if(min== INT_MAX) min=0;
    return min;
} 

posted on 2024-03-20 16:32  神奇的萝卜丝  阅读(8)  评论(0)    收藏  举报