This is an optimized naive algorithm and is only applicable when all the characters are different in the pattern.