Ctrl K

Palindrome From Two Cuts

Medium
You are given two strings left and right. Cut a substring (a contiguous piece, possibly empty) out of left and a substring (also possibly empty) out of right, then append the second piece to the end of the first. Return the greatest length of a palindrome you can build this way. A palindrome reads the same left to right and right to left.
Example 1
left="cart", right="track" → 8

The piece "cart" from left and the piece "rac" from right make "cartrac", a palindrome of length 7.

Example 2
left="xyz", right="pqr" → 1

The strings share no letters, so the best is a single letter, length 1.

Example 3
left="q", right="abba" → 4

You may take nothing from left: "abba" from right is a palindrome of length 4.

Constraints
  • 1 ≤ len(left), len(right) ≤ 30
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
loading Python…
Input
left="cart", right="track"
Expected
8

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