DSA Factory
Free Strings lessonsStrings · Stage 5 · Mastery · Step 2

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?

Ask first: which piece lengths are even possible?
s = "abcabcabcabc", length 12
a
0
b
1
c
2
a
3
b
4
c
5
a
6
b
7
c
8
a
9
b
10
c
11
check
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.

Move 1 of 4
Quick check

A string has length 12. Which piece lengths could repeat to make it exactly?

  1. A1, 2, 3, 4 and 6
  2. BAny length from 1 to 11
  3. 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).

Your problem

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.

Example
s = "abcabcabcabc" → true

0 ≤ length ≤ 10,000

Solve it in your browserFree account, no card. Python, C++, Java or JavaScript, with hints if you get stuck, and your progress is saved.
Sign up free to solve