#C4672. Divide String into Equal Parts
Divide String into Equal Parts
Divide String into Equal Parts
Given a non-empty string s
and an integer n
, your task is to divide the string into n
equal parts. More formally, if we let \(L = |s|\) be the length of the string, then a valid division exists only if \(L \mod n = 0\). In that case, each part will have a length of \(k = \frac{L}{n}\), and the parts should appear in the same order as in s
.
If the string cannot be equally divided into n
parts, output an empty array represented as []
.
Example:
- For
s = "abcdefghij"
andn = 5
, the output should be["ab", "cd", "ef", "gh", "ij"]
. - For
s = "abcde"
andn = 3
, the output should be[]
since 5 is not divisible by 3.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains the string
s
. - The second line contains the integer
n
.
Note: The string s
consists of only non-whitespace characters.
outputFormat
If the string s
can be divided into n
equal parts, print the resulting parts in order, separated by a single space, to stdout on one line. If not, print []
(without quotes).
abcdefghij
5
ab cd ef gh ij