Write a method named stutterK
that accepts a Deque
of strings and an integer k as a parameter and modifies its contents so that the first k elements in the deque each occur twice consecutively.
The relative ordering of the elements should be retained.
Do not use any other collections or arrays as auxiliary storage.
For example, if passed the deque [a, b, c, d, e, f, g, h]
, and a k of 3
, your method would change the deque to [a, a, b, b, c, c, d, e, f, g, h]
.
If you are passed a value of k that is negative or larger than the deque size, throw an IllegalArgumentException
.
This method should run in O(N) time for a deque of N elements.