Datadog interview question

Generic pattern matching medium/hard question pasted directly from leetcode.com