Engineering platform~30 min2s · 256 MBtarget O(n²) time, O(1) spacestrings
A social profile page shows a bio of lowercase letters. A design feature highlights the longest stretch of the bio that reads the same forwards and backwards, so the bio looks symmetric.
The editor can fix one letter of the highlighted stretch, changing it to any letter, to make a stretch symmetric. Your team lead asks: "How long can the highlighted stretch be if one letter may be fixed?"
Write longest_after_one_fix(bio).
bio is the profile text, lowercase letters only.bio, of length 1 or more.Input: bio = "xabzbyax"
Output: 5The longest stretch is abzby, with 5 letters. Starting from the centre at position 3, compare the mirrored letters and grow outwards. The first mismatch is fixed. A second one stops the growth.
| Left | Right | Letters | Equal? | Fixes used |
|---|---|---|---|---|
| 3 | 3 | z, z | yes | 0 |
| 2 | 4 | b, b | yes | 0 |
| 1 | 5 | a, y | no | 1 |
| 0 | 6 | x, a | no | 1, so stop |
Input: bio = "abcd"
Output: 3The stretch abc becomes aba after one fix, so it qualifies with 3 letters. The whole bio would need two fixes.
Input: bio = "q"
Output: 1A single letter always qualifies, so the answer is at least 1.
1 ≤ len(bio) ≤ 1,500bio uses only the letters a to z.Testing every stretch means about 1,500 × 1,500 / 2 = 1,125,750 stretches, and counting the mismatched pairs of each costs up to 750 more steps, about 281,531,125 steps in all. Growing outwards from each of the 2n - 1 centres, and stopping at the second mismatch, costs about 1,125,750 steps.