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
aandbof the same length. Choose an index and split both strings at the same index , splittingainto two strings:aprefixandasuffixwherea = aprefix + asuffix, and splittingbinto two strings:bprefixandbsuffixwhereb = bprefix + bsuffix. Check ifaprefix + bsuffixorbprefix + asuffixforms a palindrome.When you split a string
sintosprefixandssuffix, eitherssuffixorsprefixis allowed to be empty. For example, ifs = "abc", then"" + "abc","a" + "bc","ab" + "c", and"abc" + ""are valid splits.Return
trueif it is possible to form a palindrome string, otherwise returnfalse.Notice that
x + ydenotes the concatenation of stringsxandy.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 <= 105a.length == b.lengthaandbconsist of lowercase English letters
-
-
Constructor Summary
Constructors Constructor Description Solution()
-