Take a string of letters, digits, and hyphens. Uppercase the letters and regroup the characters with a hyphen between groups. Every group should have K characters, with one exception: the first can be shorter.
With K = 4, 5F3Z-2e-9-w becomes 5F3Z-2E9W. With K = 2, 2-5g-3-J becomes 2-5G-3J.
The phrase “except the first group” sent me down the wrong path. I took it to mean keep the first group from the input. Both examples happened to fit that reading. I found the first hyphen, stripped all the later ones, and started counting groups of K from there:
1 | def func(S,K): |
Then the judge gave me the example I wish I’d seen at the start:
1 | Input: "2-4A0r7-4k", K = 4 |
Oh. The first group in the result can be short. Nothing says the first group in the input gets to survive. In this case there are eight letters and digits, so both groups should be full. I still think the wording could have saved me a trip around the block.
Once I threw away all the hyphens, it made more sense to start from the right. Count off groups of K, put the separators where they belong, and whatever is left on the far left becomes the first group:
1 | def func(S,K): |
That one passed.




