Skip to the content

The Talavera border

Problem

Only a stretch of the Talavera tile border survived on the wall of a chapel in Puebla, and Crispín, the tile maker, wants to make the stencil that repeats it. The shorter the stencil, the fewer tiles he has to paint by hand.

Write a function that takes border, a string of lowercase letters with at least one letter: each letter is a tile. A stencil of length k works if every tile, from position k (counting from 0) to the end, is equal to the one k places before it; the stretch may end halfway through a copy. Return the smallest k that works, a whole number between 1 and the length of the string. The length always works, since no tile is left to check.

With "abcabcab": 1 does not work (the b is not equal to the a), 2 does not either (the c is not equal to the a), and 3 does, because the border reads abc, abc and ab, the last copy cut short. Return 3. With "aaaa" return 1, and with "abcd" return 4.

Examples

  • The example

    "abcabcab" → 3

  • All the tiles are the same

    "aaaa" → 1

  • No short stencil works

    "abcd" → 4

  • A single tile

    "a" → 1

  • The last copy is cut short

    "aabaa" → 3

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def shortest_stencil(border):
    pass

JavaScript

function shortestStencil(border) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →