[LeetCode Daily Question] 1616. Split two strings to get a palindrome (medium)

1616. Split two strings to get a palindrome


General aaa andbbb Two strings are cut at the same position, and the heads of each are connected to the tail of the other to form two new strings. If there is a palindrome in the two strings, return true, otherwise return false.

Analog O ( n 2 ) O(n^2)O ( n2 )timeout

The easiest thing to think of is simulation, traversing each position, cutting and assembling the string, and judging whether it is a palindrome. But enumerate each position to nnn time, judge the palindrome string to ben / 2 n/2n /2 time. Total isO ( n 2 ) O(n^2)O ( n2 ), timeout is inevitable.

class Solution {
    
    
public:
    bool checkPalindromeFormation(string a, string b) {
    
    
        int len=a.length();
        
        function<bool(string)> fun=[&](string s){
    
    
            int len=s.length();
            for(int i=0;i<(len/2);i++){
    
    
                if(s[i]!=s[len-1-i])return false;
            }
            return true;
        };

        for(int i=0;i<len;i++){
    
    
            string s1=a.substr(0,i)+b.substr(i);
            string s2=b.substr(0,i)+a.substr(i);
            if(fun(s1)||fun(s2))return true;
        }
        return false;
    }
};



Optimize AC

Let's re-examine the question. This operation is nothing more than two strings swapping tails to determine whether it is a palindrome. assuming aaa is cut off at a certain position and then connected tobbAfter the tail of b forms a palindrome string, the cut head can be further divided into "constructing a palindrome by itself" and "withbbThe tail of b forms the two parts of the "palindrome". For example

a = " p v h m    u p g q e l t o z f t l m f j j d e " a = "pvhm\;upgqeltozftlmfjjde" a="pvhmupgqeltozftlmfjjde"
b = " y j g p    z b e z s p n n p s z e b z m h v p " b = "yjgp\;zbezspnnpszebzmhvp" b="yjgpz b ezs p nn p sze b z mh v p "
The palindrome that satisfies the condition is composed ofaaa 's head andbbThe tail of b is formed, and this palindrome string can be divided into three parts:
pvhm zbezspnnpszebz mhvp pvhm\;zbezspnnpszebz\;mhvppvhmw ss p nn p sz b z _mh v p

This allows us to use two pointers to determine aa at oncea 's head andbbCan the tail of b form a palindrome?

we assume aaa on top,bbb is next. Initiallyleft = 0 left=0left=0 r i g h t = n − 1 right=n-1 right=n1 ,如果 a [ l e f t ] = = b [ r i g h t ] a[left]==b[right] a[left]==b [ r i g h t ] , the left pointer moves to the right, and the right pointer moves to the left. untila[ left ] ! = b[ right ] a[left]\;!=b[right]a[left]!=b[right] l e f t > = r i g h t left>=right left>=right

  • a [ l e f t ]    ! = b [ r i g h t ] a[left]\;!=b[right] a[left]!=b [ r i g h t ] . Means the same part is over. This is the possible cropping position, we move the pointer up or down. move up meansright rightr i g h t is the clipping position, we judge whethera [ left ] = = a [ right ] a[left]==a[right]a[left]==a [ r i g h t ] ; move down meansleft leftl e f t is the clipping position, we judge whetherb [ left ] = = b [ right ] b[left]==b[right]b[left]==b [ r i g h t ] . If it is possible untilleft >= right left>=rightleft>=r i g h t , the string is a palindrome.

  • l e f t > = r i g h t left>=right left>=r i g h t , which means the clipping position is in the middle

The above is just to judge aaa 's head andbbCan the tail of b form a palindrome? we swap places,aaa is next,bbb is above, judge againbbhead of b andaaWhether the tail of a can form a palindrome string, if one of the two is satisfied, returntrue truetrue

class Solution {
    
    
public:
    bool checkPalindromeFormation(string a, string b) {
    
    
        int len=a.length();
        
        function<bool(string,string)> fun=[&](string a,string b){
    
    
            int n=a.length();
            int i=0,j=n-1;
            while(a[i]==b[j]&&i<j) i++,j--;//找二者之间的相同部分
            if(i>=j) return true;//裁剪点在中间
            int ii=i,jj=j;
            while(b[i]==b[j]&&i<j) i++,j--;//指针下移
            if(i>=j) return true;//,整个字符串遍历完了,是回文
            while(a[ii]==a[jj]&&ii<jj) ii++,jj--;//指针上移
            if(ii>=jj) return true;
            return false;
        };

        return fun(a,b)||fun(b,a);//两种情况之一满足即可
    }
};





Bodhisattva Man【Nalan Xingde in Qing Dynasty】

The spring clouds blew away the rain in the Hunan curtain, and the fluffy butterflies flew back to live. People are in the jade building, and the height of the building is surrounded by wind.
A handful of willow tobacco shreds, dark color cage mandarin tiles. Hugh is close to the small railing, and the setting sun is infinite.

  1. Xianglian: a curtain made of Xiangfei bamboo.
  2. Yuanwa: that is, Yuanyang tile. Refers to tiles in pairs.
  3. Langan: the railing.

Guess you like

Origin blog.csdn.net/qq_44623371/article/details/129632163