code style optimization
code optimization with hashmap
more elegant recursive solution