v3 time O(kn) space O(1) Accepted