Improve speed by using a HashMap to store results