wrote Z algorithm for substring matching