All problems

Count and Say

Medium · strings

To read a digit string aloud, split it into groups of equal digits that sit next to each other. For each group, write how many digits it has, followed by the digit itself, and join these pieces in order. The count is written in ordinary decimal, so a group of twelve 7s gives 127.

For example, "1211" has the groups 1, 2 and 11, so reading it aloud gives "11" + "12" + "21" = "111221". Reading that aloud again gives "312211".

You are given a digit string s and a whole number k. Read s aloud, then read the result aloud, and so on, k times in total. Return the final string.

Starting from s = "1" this produces the well-known look-and-say terms 1, 11, 21, 1211, 111221, 312211, ...

Examples

Example 1

Input: s = "1", k = 5

Output: "312211"

The strings are 1, 11, 21, 1211, 111221, 312211, so the fifth reading is 312211.

Example 2

Input: s = "3322", k = 1

Output: "2322"

Two 3s give 23 and two 2s give 22, so the result is 2322.

Example 3

Input: s = "5555555555", k = 2

Output: "111015"

Ten 5s read aloud give 105. Reading 105 aloud gives one 1, one 0 and one 5, which is 111015.

Constraints