Trace s = "2-5g-3-J" with k = 2.
Strip dashes and uppercase: "25G3J". Length is .
5 % 2 = 1, so the first group has character: "2". Remaining: "5G3J".
Split remaining into chunks of : "5G", "3J". Join with dashes: "2-5G-3J".
You iterate the string once to strip dashes (), then once more to build groups (). That's time total.
The cleaned string and result string each take space.