This is a cool approach. I like it! Can you think of a way to decrease the space complexity of it though?