Likou 680. Verify palindrome string Ⅱ---Double pointer search (violent method and greedy method)

680. Verify palindrome string II

Given a non-empty string s, delete at most one character. Determine whether it can become a palindrome string.

Example 1:

Input: "aba"
Output: True
Example 2:

Input: "abca"
Output: True
Explanation: You can delete the c character.

注意:
字符串只包含从 a-z 的小写字母。字符串的最大长度是50000

answer:

Method 1: Two-pointer search and violence

Because the title is "Delete one at most", we can first see if a palindrome string is not deleted when it is not deleted.
If it returns directly to 1, if it is not, start the "delete" operation.
According to the violent law, you don’t need to think too much. We will delete every character and try it to see if it works. After traversing once, if there is a row, it returns 1, and if there is no row, it returns 0.

bool validPalindrome(char * s){
    
    
    int i=0,j=strlen(s)-1;
    while(i<=j)
    {
    
    
        if(s[i]==s[j])
        {
    
    
            i++;
            j--;
            continue;
        }
        break;
    }
    if(i>=j)
    {
    
    
        return 1;
    }
    for(int k=0;k<strlen(s);k++)
    {
    
    
        i=0,j=strlen(s)-1;
        while(i<=j)
        {
    
    
            if(i==k)
            {
    
    
                i++;
                continue;
            }
            if(j==k)
            {
    
    
                j--;
                continue;
            }
            if(s[i]==s[j])
            {
    
    
                i++;
                j--;
                continue;
            }
            break;
        }
        if(i>=j)
        {
    
    
            return 1;
        }
    }
    return 0;
}

But this will time out.

Method 2: Double pointer search + greedy algorithm

Find the greedy strategy :
For a given string, we can first see if the elements at the head and tail are the same. If they are not the same, the first thing we modify is either the head or the tail, and then modify it. Compare the latter, if it is found that the palindrome still cannot be made after the first modification, then return to the state before the first modification, modify the other one, and then compare;
if they are the same, the two outermost ones This can't limit him to become "not a palindrome string". So we can just see if those in it are palindrome strings.

In the same way, after we "stripped the first layer", the original second and penultimate elements became the "outermost" elements, so for them we found that the process still satisfies the former, so we still perform the above operations .
So we can find that this is actually a law.

On the code:

bool validPalindrome(char * s){
    
    
    int i=0,j=strlen(s)-1;
    int k=0,temp1,temp2;//k作为修改次数的反映,temp1和2为储存原先的状态
    while(i<=j)
    {
    
    
        if(s[i]==s[j])//相当于“剥掉”无用的元素
        {
    
    
            i++;
            j--;
            continue;
        }
        else if(s[i]!=s[j]&&k==0)//第一次修改
        {
    
    
            temp1=i;//先把原状态储存起来
            temp2=j;
            i++;//相当于“删除”操作
            k++;
            continue;
        }
        else if(s[i]!=s[j]&&k==1)//第二次修改
        {
    
    
            i=temp1;//恢复原状态
            j=temp2;
            j--;
            k++;
            continue;
        }
        else if(s[i]!=s[j]&&k>1)//修改后还是成不了回文
        {
    
    
            return 0;
        }
    }
    return 1;    
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/112325823
Recommended