You are given a string βSβ consisting of alphanumeric characters, the string divided into groups by a β-β. Your task is to reformat the string that all the groups contain exactly βKβ characters. All the lowercase characters should be converted to uppercase.
Note:The first group may have fewer characters than βKβ.
For example:
You are given βSβ =βA1-ijklmno-pqrβ and k = β3β, then the string contains 3 parts, [βA1β, βijklmnoβ, βpqrβ], then you can form the string in groups ["A1Iβ, βJKLβ, βMNOβ, βPQR"] of uppercase characters. Hence the answer is "A1I-JKL-MNO-PQR"
The first line of input contains the integer βTβ representing the number of test cases.
The first line of each test case contains one integer βKβ, representing the size of the group the string is to be divided.
The second line of each test case contains the string βSβ representing the given string.
Output Format:
For each test case, print a single string consisting of alphanumeric characters divided into groups of size βKβ separated by β-β.
Print a separate line for each test case.
1 <= T <= 10
1 <= K <= 10^8
1 <= |S| <= 10^8
All characters in βSβ are alphanumeric and β-β.
Time Limit: 1 sec
Note:
You do not need to print anything. It has already been taken care of. Just implement the function.
2
3
Ab-ijklmno-pqr
4
Isa-dkj
ABI-JKL-MNO-PQR
IS-ADKJ
For the first test case βSβ = βAb-ijklmno-pqrβ and βKβ = β3β, then the string contains 3 groups, [βAbβ, βijklmnoβ, βpqrβ], then you can form the string in groups ["ABIβ, βJKLβ, βMNOβ, βPQR"] of uppercase characters. Hence the answer is "ABI-JKL-MNO-PQR".
For the second test case βSβ = βIsa-dkjβ and βKβ = β4β, then the string contains 2 groups, [βIsaβ, βdkjβ], then you can form the string in groups ["ISβ, βADKJβ] of uppercase characters. Hence the answer is "IS-ADKJ".
2
2
a-b-1
1
abcdef
A-B1
A-B-C-D-E-F
Try to remove the separator from the string.
In this approach, we will remove the separator β-β, Then we will move from the end towards the starting of the string and add a β-β after each k character of the string.
Algorithm:
O(N), Where N is the length of the string.
We are iterating over the string once, which will take O(N) time. Hence the overall time complexity is O(N).
O(N), Where N is the length of the string.
We have to maintain the result string, which will take O(N) space in the worst case. Hence the overall space complexity is O(N).