Made of copies?
Decide whether a string is one piece repeated several times. About 10 minutes.
Narrow the choices first
When there seem to be too many possibilities to try, look for a rule that throws most of them out before you check anything at all.
Here, think about the length of the repeating piece. Tiles on a floor have to fit the room exactly. Which piece lengths could possibly fill the whole string with nothing left over?
- tries
- 1, 2, 3, 4, 6
A piece that repeats exactly must have a length that divides 12: only 1, 2, 3, 4 or 6. Everything else is ruled out for free.
A string has length 12. Which piece lengths could repeat to make it exactly?
- A1, 2, 3, 4 and 6
- BAny length from 1 to 11
- COnly 6
Show the answer
1, 2, 3, 4 and 6. A piece must fit a whole number of times, so its length divides 12 (and is less than 12).
Made of copies?
Return true if the string can be made by writing some shorter, non-empty piece two or more times in a row. Otherwise return false.
s = "abcabcabcabc" → true
0 ≤ length ≤ 10,000