completed the pattern matching problem from career cup