907. Two Odd Mirrors
A jeweller engraves a bracelet with the pattern s and wants to pick two separate stretches of beads that each read the same in both directions and each contain an odd number of beads. The two stretches must not share any position, though they may touch each other, and the first one must lie to the left of the second.
Return the largest possible product of the two stretch lengths. Every string of length at least 2 has a valid answer because a single bead is such a stretch. The product can exceed 32 bits, so return a 64-bit integer.
Aim for time O(n) where n is the length of s; trying all pairs of stretches is far too slow.
Example 1
- Input:
- s = "xxyracecarzzz"
- Output:
- 21
- Explanation:
Take 'racecar' (length 7) and the later 'zzz' (length 3): product 21; nothing better exists.
Example 2
- Input:
- s = "qrstu"
- Output:
- 1
- Explanation:
Every single letter is an odd palindrome, so two separate letters give 1 * 1 = 1.
Example 3
- Input:
- s = "abcbaxyzyx"
- Output:
- 25
- Explanation:
'abcba' (5) and 'xyzyx' (5) do not overlap: 5 * 5 = 25.
Constraints
2 ≤ s.length ≤ 80000
s contains only 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,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms
Expected complexity
- Time
- O(n)
- Space
- O(n)