String matching algorighm implementation in python