Problem 67
Repeated Substring Pattern
Easy · 10 pts · String
Problem
You are given a string S. Decide whether S can be built by taking one of its substrings and writing it two or more times in a row. For example, "abcabc" is "abc" written twice, but "aba" cannot be built this way. The repeated block must be a prefix of S, and its length must divide the length of S. So try every length L from 1 to |S| / 2 that divides |S|, and check whether repeating the first L characters |S| / L times gives S.
Input
A single string S of lowercase English letters.
Output
Print YES or NO.
Constraints
1 <= |S| <= 10000
Example input
abab
Example output
YES
Explanation
"abab" has length 4. The first 2 characters "ab" written 2 times give "abab", which equals S, so the answer is YES.