String Compression
Practice this coding question on Coding Gurukul.
Problem statement
A light controller stores a complete sequence of color codes. To save memory, it can store one block and an integer repeat count instead, provided repeating that block reconstructs the whole sequence exactly. The technician wants the shortest block that works for the supplied recording. Your task Find the shortest nonempty string p and a positive integer r such that concatenating r copies of p produces s. Print p and r. Important details The block must reproduce every character, with no leftover partial block. Using the entire input with repeat count 1 is always allowed. This is repeated-block compression, not run-length encoding of individual characters. Target O(length of s) time. Worked example For s = abcabcabc, use block abc three times. No shorter block can reconstruct the recording. For s = ababa, the apparent two-character rhythm ends with an incomplete block, so the only shortest valid representation is ababa repeated once. A string aaaa uses a four times.
Input format
Read one nonempty lowercase string s.
Output format
Print the shortest block on the first line and its repeat count on the second.
Constraints
1 <= length of s <= 10000; lowercase English letters only.
Sample input
abcabcabc
Sample output
abc 3
Sign in to run or submit a solution in C++, Java or Python.