Hone logo
Hone
Problems

String Pattern Matching with Wildcards

Pattern matching is a fundamental concept in computer science with applications ranging from text editors to network intrusion detection. This challenge asks you to implement a function that determines if a given string matches a pattern containing a wildcard character. This is a simplified version of regular expression matching, focusing on a single wildcard character for clarity.

Problem Description

You are tasked with creating a Python function matches_pattern(text, pattern) that checks if a given text string matches a pattern string. The pattern can contain a single wildcard character, represented by the question mark ?. The wildcard character can match any single character in the text. The function should return True if the text matches the pattern, and False otherwise.

Key Requirements:

  • The function must handle patterns with and without the wildcard character.
  • The function must handle cases where the pattern is longer than the text (no match).
  • The function must handle cases where the text is longer than the pattern (no match).
  • The function should be case-sensitive.

Expected Behavior:

The function should iterate through the text and pattern, comparing characters at each position. If a wildcard character ? is encountered in the pattern, it should match any character in the text at that position. If any characters do not match (and it's not a wildcard), the function should immediately return False. If the entire pattern is processed without finding a mismatch, the function should return True.

Edge Cases to Consider:

  • Empty text and/or pattern strings.
  • Pattern containing multiple wildcard characters (should be treated as invalid and return False).
  • Text and pattern of significantly different lengths.
  • Pattern starting or ending with a wildcard.

Examples

Example 1:

Input: text = "abc", pattern = "a?c"
Output: True
Explanation: The wildcard '?' in the pattern matches the character 'b' in the text.

Example 2:

Input: text = "abd", pattern = "a?c"
Output: False
Explanation: The wildcard '?' cannot match 'd', so the pattern does not match the text.

Example 3:

Input: text = "abc", pattern = "abcd"
Output: False
Explanation: The pattern is longer than the text, so there's no match.

Example 4:

Input: text = "abc", pattern = "ab?"
Output: True
Explanation: The wildcard '?' matches 'c'.

Example 5:

Input: text = "abc", pattern = "a??c"
Output: False
Explanation: The pattern contains multiple wildcards, which is invalid.

Constraints

  • The length of the text string will be between 0 and 1000 characters (inclusive).
  • The length of the pattern string will be between 0 and 1000 characters (inclusive).
  • Both text and pattern will consist of lowercase English letters.
  • The pattern should contain at most one ? character. If it contains more, the function should return False.
  • The function should have a time complexity of O(n), where n is the length of the shorter string (text or pattern).

Notes

Consider using a simple iterative approach to compare the text and pattern character by character. Pay close attention to the lengths of the strings and handle the wildcard character appropriately. Think about how to efficiently detect and handle the case where the pattern contains more than one wildcard. A good approach is to check for multiple wildcards before starting the main comparison loop.

Loading editor...
python