Skip to content

is_match algorithm #12290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions dynamic_programming/is_match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""You are given a string s and a pattern p. Write a function to determine if the pattern p matches the entire string s.

Check failure on line 1 in dynamic_programming/is_match.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/is_match.py:1:89: E501 Line too long (120 > 88)
The pattern p may contain the special characters:"""

""" ? which matches any single character.
* which matches any sequence of characters (including the empty sequence).
Return True if p matches the entire string s; otherwise, return False."""

"""Example :
Input: s = "aa", p = "a"
Output: False
Explanation: The pattern "a" does not match the entire string "aa"."""


def is_match(s, p):
m, n = len(s), len(p)

# Initialize a DP table where dp[i][j] represents whether s[:i] matches p[:j]
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True # Empty pattern matches empty string

# Fill the first row (when s is empty)
for j in range(1, n + 1):
if p[j - 1] == "*":
dp[0][j] = dp[0][j - 1] # '*' can match an empty sequence

# Fill the DP table
for i in range(1, m + 1):
for j in range(1, n + 1):
if p[j - 1] == "*":
# '*' can match zero or more of the previous characters
dp[i][j] = dp[i - 1][j] or dp[i][j - 1]
elif p[j - 1] == "?" or s[i - 1] == p[j - 1]:
# '?' matches any single character, or if characters match exactly
dp[i][j] = dp[i - 1][j - 1]

# The result is whether the entire string s matches the entire pattern p
return dp[m][n]


# Take user input for string and pattern
s = input("Enter the string: ")
p = input("Enter the pattern: ")

# Print the result
print("Does the pattern match the string?", is_match(s, p))
Loading