Ctrl K

Palindrome With a Borrowed Tail

Medium
You are given two strings first and second of the same length. Pick a cut position and cut both strings at that same spot, so each splits into a head and a tail (either part may be empty). Then glue the head of one string to the tail of the other: the head of first with the tail of second, or the head of second with the tail of first. Return True if for at least one cut position one of the two glued strings is a palindrome, and False otherwise.
Example 1
first='kayxq', second='plbak' → True

Cut after the third letter: kay + ak = kayak, which is a palindrome.

Example 2
first='abc', second='dbe' → False

No cut point turns either the head of first plus the tail of second, or the other way round, into a palindrome.

Example 3
first='q', second='w' → True
Constraints
  • 1 ≤ len(first) = len(second) ≤ 10⁵
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumWords in Reverse Order MediumExactly One Edit Apart MediumWhich Release Is Newer MediumReverse the Word Order in Place Medium
loading Python…
Input
first='kayxq', second='plbak'
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.