Problem 532568 · medium · Phase 05 Advanced Algorithms & Graphs

Mirror Banner

dynamic programming · interval DP · palindromes · subsequences

A sign painter has a banner with the letters of s. She may paint over any letters (the rest keep their order) and wants what remains to read the same forwards and backwards. Return the largest number of letters that can remain.

Examples

Input:  s = "character"
Output: 5
Explanation: keep "carac".

Input:  s = "abcd"
Output: 1

Constraints

  • 1 <= len(s) <= 300
  • s consists of lowercase letters.
  • There are 2**300 ways to choose letters to keep; aim for O(n**2).

Goals

  • Define a table over substrings s[i..j] and fill it by increasing length
  • Match the two outer characters or drop one of them
Starting Python…