A polynomial time algorithm for subpattern matching