808. Fewest Mirror Cuts
A jeweller threads beads marked with lowercase letters into a necklace described by the string s. The thread is cut at some of the gaps between neighbouring beads so that every resulting piece is a mirror: it reads the same from left to right and from right to left.
Return the smallest number of cuts that achieves this. A necklace that already reads the same both ways needs 0 cuts, and a single bead is always a mirror on its own, so an answer always exists.
Aim for time O(n^2) and extra space O(n), where n is the length of s.
Example 1
- Input:
- s = "tenetkayak"
- Output:
- 1
- Explanation:
The thread splits into the mirror pieces tenet and kayak, so a single cut is enough.
Example 2
- Input:
- s = "rotorxyq"
- Output:
- 3
- Explanation:
rotor is a mirror but the three letters after it are not joined to anything, so the cuts go rotor | x | y | q: three cuts.
Constraints
1 ≤ s.length ≤ 2000
s consists of lowercase English letters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,000 msC++ 250 msJava 500 msJavaScript 500 msTypeScript 500 ms
Expected complexity
- Time
- O(n^2)
- Space
- O(n)