Longest Palindrome
Real-world version: Longest Lintel Row of Carved TilesThe statement here is our own: the same inputs, outputs and technique.
A mason keeps carved letter tiles loose in a crate. crate gives the carving on each tile, in the order the tiles come out of the crate. Carvings are cut with either an upper-case punch or a lower-case punch, and the two are different tiles: a tile carved S will not pass for a tile carved s.
The mason lays some of the tiles in one row along a lintel. The tiles may be laid in any order, any number of them may be left in the crate, and no tile can be used twice. The row must read the same scanned left to right as scanned right to left.
Return the largest number of tiles such a row can contain.
Examples
Example 1
- Input
- crate = "ledgerledger"
- Output
- 12
All twelve tiles go into the row, for example laid as `ledgerregdel`, which reads the same from either end.
Example 2
- Input
- crate = "Zz"
- Output
- 1
The two tiles carry different carvings, so the row holds a single tile, and one tile reads the same either way.
Example 3
- Input
- crate = "ppqr"
- Output
- 3
A row of three such as `pqp` uses both `p` tiles and one of the remaining two; the last tile stays in the crate.
Example 4
- Input
- crate = "granitegranit"
- Output
- 13
Thirteen of the tiles form a row, for instance `granitetinarg`.
Constraints
- 1 <= crate.length <= 2000
- crate consists of lower-case and/or upper-case English letters only.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_band(crate: str) -> int:public int longestBand(String crate)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Reorganize StringReal-world version: Paint Line ShuffleMedium
- Using a Robot to Print the Lexicographically Smallest StringReal-world version: Typesetter's RackMedium
- Partition LabelsReal-world version: Duty Roster BlocksMedium
- Maximum Palindromes After OperationsReal-world version: Noticeboard Word StripsMedium