If you don't mind using BioPerl, you can try Bio::Tools::SeqPattern module for this purpose http://doc.bioperl.org/releases/bioperl-1.4/Bio/Tools/SeqPattern.html
Hope helpful.
BestShreya
I don't have any code for you, but you can find several useful links here http://bioinformaticsonline.com/bookmarks/view/4098/bioinformatics-algorithm-demonstrations-and-tutorials which might help you to resolve the problems.
Note: If you are not...
I found some good resources from AI articles, specifically from McGill University's COMP-644 (Pattern Recognition) course.
http://cgm.cs.mcgill.ca/~godfried/teaching/pr-web.html