Python String Matching Algorithm Code