• 1616. 分割两个字符串得到回文串


    给你两个字符串 a 和 b ,它们长度相同。请你选择一个下标,将两个字符串都在 相同的下标 分割开。由 a 可以得到两个字符串: aprefix 和 asuffix ,满足 a = aprefix + asuffix ,同理,由 b 可以得到两个字符串 bprefix 和 bsuffix ,满足 b = bprefix + bsuffix 。请你判断 aprefix + bsuffix 或者 bprefix + asuffix 能否构成回文串。

    当你将一个字符串 s 分割成 sprefix 和 ssuffix 时, ssuffix 或者 sprefix 可以为空。比方说, s = "abc" 那么 "" + "abc" , "a" + "bc" , "ab" + "c" 和 "abc" + "" 都是合法分割。

    如果 能构成回文字符串 ,那么请返回 true,否则返回 false 。

    请注意, x + y 表示连接字符串 x 和 y 。

    示例 1:

    输入:a = "x", b = "y"
    输出:true
    解释:如果 a 或者 b 是回文串,那么答案一定为 true ,因为你可以如下分割:
    aprefix = "", asuffix = "x"
    bprefix = "", bsuffix = "y"
    那么 aprefix + bsuffix = "" + "y" = "y" 是回文串。
    示例 2:

    输入:a = "ulacfd", b = "jizalu"
    输出:true
    解释:在下标为 3 处分割:
    aprefix = "ula", asuffix = "cfd"
    bprefix = "jiz", bsuffix = "alu"
    那么 aprefix + bsuffix = "ula" + "alu" = "ulaalu" 是回文串。
     

    提示:

    1 <= a.length, b.length <= 105
    a.length == b.length
    a 和 b 都只包含小写英文字母

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/split-two-strings-to-make-palindrome

    class Solution:
        def checkPalindromeFormation(self, a: str, b: str) -> bool:
            def pal(s,i,j):
                while i<j:
                    if s[i]!=s[j]:
                        return False
                    i+=1
                    j-=1
                return True
    
            def is_pal(a, b):
                i = 0
                j = len(b) - 1
                while i<j:
                    if a[i]!=b[j]:
                        return pal(a,i,j) or pal(b,i,j)
                    i+=1
                    j-=1
                return True
                
            return is_pal(a,b) or is_pal(b,a)

  • 相关阅读:
    关于 Profile
    empty
    Vim Editor
    C++ Note
    Android NDK Sample
    Dealing with the ! when you import android project
    File attributes and Authority of Linux
    Java与C的相互调用
    The source code list of Android Git project
    Enable Android progurad in the case of multiple JAR lib
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13797231.html
Copyright © 2020-2023  润新知