[LeetCode] 1616. 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 + 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 = "abdef", b = "fecab" Output: true
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.
Example 4:
Input: a = "xbdef", b = "xecab" Output: false
Constraints:
1 <= a.length, b.length <= 105
a.length == b.length
a
andb
consist of lowercase English letters
分割两个字符串得到回文串。
给你两个字符串 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 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/split-two-strings-to-make-palindrome
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是给两个长度相同的字符串a和b,从a的某个index切一刀,拿出来一个前缀prefix,从b的同一个index切一刀拿出来一个后缀suffix,问a prefix + b suffix是否能拼接成一个回文字符串。
思路是一步步往前走,判断切割出来的部分是否是回文,但是注意这道题可以从a拿出来一个前缀,从b拿出来一个后缀,也可以从b拿出来一个前缀,从a拿出来一个后缀。判断的时候两种情况都需要判断。跑个例子,比如
a = "abc dace cba"
b = "abc dace cba"
用两个指针,一个从a的左边开始扫描起,一个从b的右边开始扫描起,判断两者指向的字母是否相同。加粗的部分是a的前缀和b的后缀,他们之间可以组成回文串"abccba"。此时a的指针会停在d,b的指针会停在e。但是按照题目规则,如果此时停在这里,应该切割的是a的前缀abc和b的前缀abc,b应该拿出来后缀dacecba来和abc(来自a)拼接。或者是拿a此时的后缀dacecba与b的前缀abc拼接。因为一开始加粗的部分已经互相成回文了,所以我们只需要判断中间的dace是否为回文即可。
时间O(n)
空间O(1)
Java实现
1 class Solution { 2 public boolean checkPalindromeFormation(String a, String b) { 3 return check(a, b) || check(b, a); 4 } 5 6 private boolean check(String a, String b) { 7 int i = 0; 8 int j = a.length() - 1; 9 while (i < j && a.charAt(i) == b.charAt(j)) { 10 i++; 11 j--; 12 } 13 return isPalindrome(a, i, j) || isPalindrome(b, i, j); 14 } 15 16 private boolean isPalindrome(String s, int i, int j) { 17 while (i < j && s.charAt(i) == s.charAt(j)) { 18 i++; 19 j--; 20 } 21 return i >= j; 22 } 23 }