Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Regex Word Search

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Regex Word Search. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Docusign may need to match user-entered patterns against words used in template names or document metadata. Given a dictionary of words and a pattern, determine whether at least one complete word matches the pattern.

The pattern supports:

  1. A lowercase letter, which matches itself.
  2. ., which matches any single lowercase letter.
  3. *, which must follow a letter or . and matches zero or more occurrences of that preceding element.

A match must consume the entire word, not just a prefix or substring.

Formal Specification

Implement pattern_exists(words, pattern).

  • Input: words, a list of lowercase strings, and pattern, a lowercase pattern string containing letters, ., and *.
  • Output: Return True if any word matches the complete pattern; otherwise return False.
  • The dictionary may contain duplicate words. The pattern will never begin with * or contain consecutive * characters.

Constraints

  • 1 <= len(words) <= 10^4
  • 1 <= len(word) <= 1000
  • 0 <= len(pattern) <= 1000
  • Words contain only lowercase English letters
  • The pattern contains lowercase letters, '.', and '*'
  • The pattern never begins with '*' or contains consecutive '*' characters

Function Signature

def pattern_exists(words, pattern):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output