v2 time O(n) space O(n) Accepted

didn't think carefully at all in v1. the algorithm was incorrect. should be cautious at the seemingly easy questions.

btw, IllegalArgumentException is in java.lang so no need to import and switch doesn't support String