-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add wildcard pattern matching using dynamic programming #5334
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 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d441a4a
Added regular expression implimentation using dp
punithbajaj 713d919
replaced input() with example values
punithbajaj 23ca312
Merge branch 'master' of https://github.com/TheAlgorithms/Python
punithbajaj aca3295
Apply suggestions from code review
punithbajaj f54d11c
changed returning value to bool and added test cases
punithbajaj d5c433a
Merge branch 'master' of https://github.com/punithbajaj/Python
punithbajaj 1b298b2
added doctest
punithbajaj 60a7b78
added test cases
punithbajaj c4d5971
Apply suggestions from code review
punithbajaj 6e8e8c1
shifted to strings
punithbajaj 77c0357
Changed filename
punithbajaj e096288
Update function name to match_pattern
punithbajaj 459c806
Update function name to match_pattern
punithbajaj d274ddc
Merge branch 'master' of https://github.com/punithbajaj/Python
punithbajaj 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,103 @@ | ||
""" | ||
Implementation of regular expression matching with support for '.' and '*'. | ||
'.' Matches any single character. | ||
'*' Matches zero or more of the preceding element. | ||
The matching should cover the entire input string (not partial). | ||
|
||
""" | ||
|
||
|
||
def string_match_pattern(input_string: str, pattern: str) -> bool: | ||
""" | ||
using bottom-up dynamic programming solution for matching the input | ||
string with a given pattern. | ||
|
||
Runtime: O(len(input_string)*len(pattern)) | ||
|
||
Arguments | ||
-------- | ||
input_string: str, any string which should be compared with pattern | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pattern: str, the string that has to be used as pattern and should contain | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'.' for single character match and '*' for zero or more of preceding character | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
match | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Note | ||
---- | ||
the pattern can not start with a '*', | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
because there should be at least one character before * | ||
|
||
Returns | ||
------- | ||
the bool value denoting whether given string follows the pattern | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Examples | ||
------- | ||
>>> string_match_pattern("aab", "c*a*b") | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
True | ||
>>> string_match_pattern("aaa", "aa") | ||
False | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> string_match_pattern("aaab", "aa*") | ||
False | ||
>>> string_match_pattern("aaab", ".*") | ||
True | ||
>>> string_match_pattern("a", "bbbb") | ||
False | ||
>>> string_match_pattern("", "bbbb") | ||
False | ||
>>> string_match_pattern("a", "") | ||
False | ||
""" | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
len_string = len(input_string) + 1 | ||
len_pattern = len(pattern) + 1 | ||
|
||
# dp is a 2d matrix where dp[i][j] denotes whether prefix string of | ||
# length i of input_string matches with prefix string of length j of | ||
# given pattern | ||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dp = [[0 for i in range(len_pattern)] for j in range(len_string)] | ||
|
||
# since string of zero length match pattern of zero length | ||
dp[0][0] = 1 | ||
|
||
# since pattern of zero length will never match with string of non-zero length | ||
for i in range(1, len_string): | ||
dp[i][0] = 0 | ||
|
||
# since string of zero length will match with pattern where there | ||
# is at least one * alternatively | ||
for j in range(1, len_pattern): | ||
dp[0][j] = dp[0][j - 2] if pattern[j - 1] == "*" else 0 | ||
|
||
# now using bottom-up approach to find for all remaining lengths | ||
for i in range(1, len_string): | ||
for j in range(1, len_pattern): | ||
if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": | ||
dp[i][j] = dp[i - 1][j - 1] | ||
|
||
elif pattern[j - 1] == "*": | ||
if dp[i][j - 2] == 1: | ||
dp[i][j] = 1 | ||
elif pattern[j - 2] in (input_string[i - 1], "."): | ||
dp[i][j] = dp[i - 1][j] | ||
else: | ||
dp[i][j] = 0 | ||
else: | ||
dp[i][j] = 0 | ||
|
||
return bool(dp[-1][-1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
punithbajaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# inputing the strings | ||
# input_string = input("input a string :") | ||
# pattern = input("input a pattern :") | ||
|
||
input_string = "aab" | ||
pattern = "c*a*b" | ||
|
||
# using function to check whether given string matches the given pattern | ||
if string_match_pattern(input_string, pattern): | ||
print(f"{input_string} matches the given pattern {pattern}") | ||
else: | ||
print(f"{input_string} does not match with the given pattern {pattern}") |
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.