Optimized the alternate way to calculate pattern.