oh and I forgot to mention: I wouldn't worry too much about memory and efficiency of the algorithm unless you plan to implement this on a web server or something where it will run all the time. It's more likely to be a one-off and, especially as a beginner, you can easily spend much more time optimising it than you will ever save. If it takes a bit longer, use the time to brew a nice cup of coffee or go home and find the shiny new results waiting on your disk for you in the morning... :-)