472. Maximum Coloured Circles in a Palindrome Pattern
Maximum Coloured Circles in a Palindrome Pattern

A page in a colouring book has a row of coloured circles. You are given a string circleColours, where each letter is the colour of one circle, from left to right. For example, 'r' is red and 'b' is blue.

You may erase any circles. You cannot move or swap circles.

The circles you keep must make a palindrome pattern: their colours read the same from left to right and from right to left.

Return the maximum number of circles you can keep.

Method Signature

int maxCirclesKept(String circleColours)
  • circleColours: the colours of the circles from left to right, one letter per circle
  • Returns the maximum number of circles that can be kept so that they make a palindrome pattern

Constraints

  • 1 ≤ circleColours.length() ≤ 1,000
  • circleColours contains only lowercase English letters

Examples

Example 1

Input: maxCirclesKept(circleColours = "rgybgr")

Output: 5

Explanation: Erase the blue circle. The kept circles are "rgygr", which reads the same from both sides.

Example 2

Input: maxCirclesKept(circleColours = "yrgbgro")

Output: 5

Explanation: Erase the first circle (yellow) and the last circle (orange). The kept circles are "rgbgr".

Example 3

Input: maxCirclesKept(circleColours = "rgbyo")

Output: 1

Explanation: All colours are different, so only one circle can be kept. One circle on its own is a palindrome pattern.

Example 4

Input: maxCirclesKept(circleColours = "rbwwbr")

Output: 6

Explanation: The row is already a palindrome pattern, so no circle is erased.



Please use Laptop/Desktop or any other large screen to add/edit code.