Problem · String

Split a String into Three Palindromes

Learn this problem
MediumOracle logoOracleFULLTIMEONSITE INTERVIEW

Problem statement

Given a string s, return true if it can be split into exactly three non-empty contiguous substrings such that every substring is a palindrome. Otherwise, return false.

A palindrome reads the same from left to right and right to left. The three substrings must preserve the original order and together use every character of s exactly once.

Function

canSplitIntoThreePalindromes(s: String) → boolean

Examples

Example 1

s = "abcbdd"return = true

One valid split is "a" | "bcb" | "dd". All three pieces are non-empty palindromes.

Example 2

s = "bcbddxy"return = false

No placement of two split points makes all three resulting substrings palindromes.

Example 3

s = "aaa"return = true

The only valid three-part split is "a" | "a" | "a", and each one-character substring is a palindrome.

Constraints

  • 3 <= s.length <= 2000.
  • s contains only lowercase English letters.

More Oracle problems

drafts saved locally
public boolean canSplitIntoThreePalindromes(String s) {
    // Write your code here.
}
s"abcbdd"
expectedtrue
checking account