All problems
0443EasyHash TableStringGreedy

Longest Lintel Row of Carved Tiles

Tracked in this browser only
Write code

Trains the technique from

LeetCode 409Longest Palindrome

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def longest_band(crate: str) -> int:
Java
public int longestBand(String crate)
September 7
Apply