Class Solution
- java.lang.Object
-
- g1601_1700.s1616_split_two_strings_to_make_palindrome.Solution
-
public class Solution extends Object
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:aprefix
andasuffix
wherea = aprefix + asuffix
, and splittingb
into two strings:bprefix
andbsuffix
whereb = bprefix + bsuffix
. Check ifaprefix + bsuffix
orbprefix + asuffix
forms a palindrome.When you split a string
s
intosprefix
andssuffix
, eitherssuffix
orsprefix
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:
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
andb
consist of lowercase English letters
-
-
Constructor Summary
Constructors Constructor Description Solution()
-