#K55262. String Compression with Frequency Threshold
String Compression with Frequency Threshold
String Compression with Frequency Threshold
Given a string S
and an integer N
, compress the string by replacing every consecutive group of the same character that appears at least N
times with the character followed by its count. Otherwise, the group is left unchanged.
Formally, let a group of consecutive characters be represented as a substring where all characters are identical and its length is L. If L \geq N, the group is replaced by character + L
in the output. Otherwise, the group remains as it is in the original string.
For example, if S = "aaabccccd"
and N = 3
, the compressed string is a3bc4d
because the group 'aaa' (length 3) is compressed, and the group 'cccc' (length 4) is also compressed.
inputFormat
Input is provided via standard input (stdin). The first line contains the string S
. The second line contains the integer N
.
outputFormat
Output the compressed string to standard output (stdout).
## sampleaaabccccd
3
a3bc4d