-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add: Two Regex match algorithm (Recursive & DP) #6321
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ba296e1
Add recursive solution to regex_match.py
itsamirhn 0cd88af
Add dp solution to regex_match.py
itsamirhn 8d65acf
Add link to regex_match.py
itsamirhn e71795c
Minor edit
itsamirhn 6e424f3
Minor change
itsamirhn d224cfd
Minor change
itsamirhn b87b9a8
Merge branch 'TheAlgorithms:master' into regex-match
itsamirhn c2bd6bf
Update dynamic_programming/regex_match.py
itsamirhn 7271e3d
Update dynamic_programming/regex_match.py
itsamirhn 4456366
Fix ruff formatting in if statements
itsamirhn 8c5aef0
Update dynamic_programming/regex_match.py
itsamirhn 475b5b5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
""" | ||
Regex matching check if a text matches pattern or not. | ||
Pattern: | ||
'.' Matches any single character. | ||
'*' Matches zero or more of the preceding element. | ||
More info: | ||
https://medium.com/trick-the-interviwer/regular-expression-matching-9972eb74c03 | ||
""" | ||
|
||
|
||
def recursive_match(text: str, pattern: str) -> bool: | ||
""" | ||
Recursive matching algorithm. | ||
|
||
Time complexity: O(2 ^ (|text| + |pattern|)) | ||
Space complexity: Recursion depth is O(|text| + |pattern|). | ||
|
||
:param text: Text to match. | ||
:param pattern: Pattern to match. | ||
:return: True if text matches pattern, False otherwise. | ||
|
||
>>> recursive_match('abc', 'a.c') | ||
True | ||
>>> recursive_match('abc', 'af*.c') | ||
True | ||
>>> recursive_match('abc', 'a.c*') | ||
True | ||
>>> recursive_match('abc', 'a.c*d') | ||
False | ||
>>> recursive_match('aa', '.*') | ||
True | ||
""" | ||
if not text and not pattern: | ||
return True | ||
|
||
if text and not pattern: | ||
return False | ||
|
||
if not text and pattern and pattern[-1] != "*": | ||
return False | ||
|
||
if not text and pattern and pattern[-1] == "*": | ||
return recursive_match(text, pattern[:-2]) | ||
itsamirhn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if text[-1] == pattern[-1] or pattern[-1] == ".": | ||
return recursive_match(text[:-1], pattern[:-1]) | ||
|
||
if pattern[-1] == "*": | ||
return recursive_match(text[:-1], pattern) or recursive_match( | ||
text, pattern[:-2] | ||
) | ||
|
||
return False | ||
|
||
|
||
def dp_match(text: str, pattern: str) -> bool: | ||
""" | ||
Dynamic programming matching algorithm. | ||
|
||
Time complexity: O(|text| * |pattern|) | ||
Space complexity: O(|text| * |pattern|) | ||
|
||
:param text: Text to match. | ||
:param pattern: Pattern to match. | ||
:return: True if text matches pattern, False otherwise. | ||
|
||
>>> dp_match('abc', 'a.c') | ||
True | ||
>>> dp_match('abc', 'af*.c') | ||
True | ||
>>> dp_match('abc', 'a.c*') | ||
True | ||
>>> dp_match('abc', 'a.c*d') | ||
False | ||
>>> dp_match('aa', '.*') | ||
True | ||
""" | ||
m = len(text) | ||
n = len(pattern) | ||
dp = [[False for _ in range(n + 1)] for _ in range(m + 1)] | ||
dp[0][0] = True | ||
|
||
for i in range(1, m + 1): | ||
dp[i][0] = False | ||
itsamirhn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for j in range(1, n + 1): | ||
dp[0][j] = pattern[j - 1] == "*" and dp[0][j - 2] | ||
|
||
for i in range(1, m + 1): | ||
for j in range(1, n + 1): | ||
if pattern[j - 1] == "." or pattern[j - 1] == text[i - 1]: | ||
dp[i][j] = dp[i - 1][j - 1] | ||
elif pattern[j - 1] == "*": | ||
dp[i][j] = dp[i][j - 2] | ||
if pattern[j - 2] == "." or pattern[j - 2] == text[i - 1]: | ||
dp[i][j] |= dp[i - 1][j] | ||
else: | ||
dp[i][j] = False | ||
|
||
return dp[m][n] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.