代码编织梦想

Description

You are given two strings a and b of the same length. Choose an index and split both strings at the same index, splitting a into two strings: aprefix and asuffix where a = aprefix + asuffix, and splitting b into two strings: bprefix and bsuffix where b = bprefix + bsuffix. Check if aprefix + bsuffix or bprefix + asuffix forms a palindrome.

When you split a string s into sprefix and ssuffix, either ssuffix or sprefix is allowed to be empty. For example, if s = “abc”, then “” + “abc”, “a” + “bc”, “ab” + “c” , and “abc” + “” are valid splits.

Return true if it is possible to form a palindrome string, otherwise return false.

Notice that x + y denotes the concatenation of strings x and y.

Example 1:

Input: a = "x", b = "y"
Output: true
Explaination: If either a or b are palindromes the answer is true since you can split in the following way:
aprefix = "", asuffix = "x"
bprefix = "", bsuffix = "y"
Then, aprefix + bsuffix = "" + "y" = "y", which is a palindrome.

Example 2:

Input: a = "xbdef", b = "xecab"
Output: false

Example 3:

Input: a = "ulacfd", b = "jizalu"
Output: true
Explaination: Split them at index 3:
aprefix = "ula", asuffix = "cfd"
bprefix = "jiz", bsuffix = "alu"
Then, aprefix + bsuffix = "ula" + "alu" = "ulaalu", which is a palindrome.

Constraints:

1 <= a.length, b.length <= 105
a.length == b.length
a and b consist of lowercase English letters

Solution

The palindrome would only be like:
在这里插入图片描述
So get two pointers, one working on A, one working at B reversely, if the char that are pointed are not the same, then either change pointer A to work on B or change pointer B to work on A.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)

Code

class Solution:
    def checkPalindromeFormation(self, a: str, b: str) -> bool:
        def checker(a: str, b: str) -> bool:
            a_index, b_index = 0, -1
            cmp_a, cmp_b = a, b
            # cmp_a -> b
            flag_a_to_b, flag_b_to_a = True, True
            while a_index - b_index < len(b):
                if cmp_a[a_index] == cmp_b[b_index]:
                    a_index += 1
                    b_index -= 1
                else:
                    if cmp_a == a:
                        cmp_a = b
                    else:
                        flag_a_to_b = False
                        break
            # cmp_b -> a
            cmp_a, cmp_b = a, b
            a_index, b_index = 0, -1
            while a_index - b_index < len(b):
                if cmp_a[a_index] == cmp_b[b_index]:
                    a_index += 1
                    b_index -= 1
                else:
                    if cmp_b == b:
                        cmp_b = a
                    else:
                        flag_b_to_a = False
                        break
            return flag_a_to_b or flag_b_to_a
        return checker(a, b) or checker(b, a)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_41679123/article/details/129643143

LeetCode目录-爱代码爱编程

实时更新中。 文章目录 1.C语言版2.C++语言版 1.C语言版 1.LeetCode:1. Two Sum 两数之和 2.LeetCode:2. Add Two Numbers 两数相加 3.LeetCode:3. Longest Substring Without Repeating Characters 无重复字符的最长子串 4.Le

【leetcode】解题日记(未完待续)-爱代码爱编程

开坑,有生之年系列,希望有一天能解出 l e e t c

Leetcode题目练习总结(持续更新......)-爱代码爱编程

Leetcode题目练习 数组1.两数之和26. 删除排序数组中的重复项27. 移除元素35.搜索插入位置53.最大子序列66.加一88.合并两个有序数组118.杨辉三角119.杨辉三角2121.买卖股票的最佳时机122.买卖股票的最佳时机2167.两数之和2169.多数元素189.旋转数组217.存在重复元素219.存在重复元素2268.缺失数字

1616. 分割两个字符串得到回文串-爱代码爱编程

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

Leetcode 1616. Split Two Strings to Make Palindrome (python)-爱代码爱编程

Leetcode 1616. Split Two Strings to Make Palindrome 题目:解法1:暴力,TLE解法2:O(n) 题目: 解法1:暴力,TLE class Solution: def checkPalindromeFormation(self, a: str, b: str) -> boo

[leetcode] 1616. Split Two Strings to Make Palindrome-爱代码爱编程

Description You are given two strings a and b of the same length. Choose an index and split both strings at the same index, splitting a into two strings: aprefix and asuffix wher

AK F.*ing leetcode 流浪计划之回文串-爱代码爱编程

欢迎关注更多精彩 关注我,学习常用算法与数据结构,一题多解,降维打击。 文章目录 一、简介二、解题步骤三、作用四、经典算法介绍判断一个串是否为回文串(单次查询)普通情况判断指定字符多次子串查询动态规划法O(n^2)中心扩展法O(n^2)Manacher(马拉车算法) O(n)五、牛刀小试练习1 [最长回文子串](https://leetcode-

LeetCode每日一题(Split Two Strings to Make Palindrome)-爱代码爱编程

You are given two strings a and b of the same length. Choose an index and split both strings at the same index, splitting a into two strings: aprefix and asuffix where a = aprefix

【Leetcode】1616. Split Two Strings to Make Palindrome-爱代码爱编程

题目地址: https://leetcode.com/problems/split-two-strings-to-make-palindrome/ 给定两个长度都是 n n n的字符串

leetcode刷题规划-爱代码爱编程

LeetCode精华题目列表【刷题规划系列】 – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advanced Data Structures HashSet / HashTable Tree Heap Graph (Breadth

后端开发——算法题-爱代码爱编程

文章目录 字节腾讯百度阿里美团快手 字节 题目出现次数链接25. K 个一组翻转链表60https://leetcode-cn.com/problems/reverse-nodes-in-k-group3. 无重复字符的最长子串57https://leetcode-cn.com/problems/longest-substring-witho

1616. split two strings to make palindrome_boligongzhu的博客-爱代码爱编程

You are given two strings a and b of the same length. Choose an index and split both strings at the same index, splitting a into two strings: aprefix and asuffix where a = aprefix

acwing leetcode 题目分类——配套基础课进阶课_啊枫_jiangchufeng的博客-爱代码爱编程

LeetCode 题目分类——配套基础课进阶课 1.基础 二分(满足一个条件的最值问题) LeetCode33 https://leetcode.com/problems/search-in-rotated-sorte

leetcode 1221. split a string in balanced strings-爱代码爱编程

Balanced strings are those that have an equal quantity of 'L' and 'R' characters. Given a balanced string s, split it into some number of substrings such that: Each substring is

lc-1616. 分割两个字符串得到回文串(相向双指针、中心扩展)-爱代码爱编程

文章目录 [1616. 分割两个字符串得到回文串](https://leetcode.cn/problems/split-two-strings-to-make-palindrome/)暴力(超时)相向双指针优化法二