Class Solution
-
- All Implemented Interfaces:
public final class Solution
1616 - Split Two Strings to Make Palindrome.
Medium
You are given two strings
a
andb
of the same length. Choose an index and split both strings at the same index , splittinga
into two strings: <code>a<sub>prefix</sub></code> and <code>a<sub>suffix</sub></code> where <code>a = a<sub>prefix</sub> + a<sub>suffix</sub></code>, and splittingb
into two strings: <code>b<sub>prefix</sub></code> and <code>b<sub>suffix</sub></code> where <code>b = b<sub>prefix</sub> + b<sub>suffix</sub></code>. Check if <code>a<sub>prefix</sub> + b<sub>suffix</sub></code> or <code>b<sub>prefix</sub> + a<sub>suffix</sub></code> forms a palindrome.When you split a string
s
into <code>s<sub>prefix</sub></code> and <code>s<sub>suffix</sub></code>, either <code>s<sub>suffix</sub></code> or <code>s<sub>prefix</sub></code> is allowed to be empty. For example, ifs = "abc"
, then"" + "abc"
,"a" + "bc"
,"ab" + "c"
, and"abc" + ""
are valid splits.Return
true
if it is possible to form a palindrome string, otherwise returnfalse
.Notice that
x + y
denotes the concatenation of stringsx
andy
.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:
a<sub>prefix</sub> = "", a<sub>suffix</sub> = "x"
b<sub>prefix</sub> = "", b<sub>suffix</sub> = "y"
Then, a<sub>prefix</sub> + b<sub>suffix</sub> = "" + "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:
a<sub>prefix</sub> = "ula", a<sub>suffix</sub> = "cfd"
b<sub>prefix</sub> = "jiz", b<sub>suffix</sub> = "alu"
Then, a<sub>prefix</sub> + b<sub>suffix</sub> = "ula" + "alu" = "ulaalu", which is a palindrome.
Constraints:
<code>1 <= a.length, b.length <= 10<sup>5</sup></code>
a.length == b.length
a
andb
consist of lowercase English letters
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final Boolean
checkPalindromeFormation(String a, String b)
-
-
Method Detail
-
checkPalindromeFormation
final Boolean checkPalindromeFormation(String a, String b)
-
-
-
-