Skip to the code
cf.
+25 XP

#543 Longest Mirror Subsequence

medium

A word game highlights the longest "mirror" hidden in a string s: a subsequence (characters kept in order, others skipped) that reads the same forward and backward.

Write longestMirror(s) that returns the length of the longest palindromic subsequence of s.

Example 1

Input: s = "racecars"
Output: 7

racecar: skip the final s.

Example 2

Input: s = "abcd"
Output: 1

No letter repeats, so any single letter is the longest mirror.

Constraints

  • 1 <= s.length <= 1000
  • s contains only lowercase English letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).