-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Match a pattern and String using backtracking #9861
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
Changes from 26 commits
4e9df4a
1fab302
8704157
143764d
045652d
b5fb9f5
95161a0
155601c
376cb60
6a424b3
a878e4b
2b096d9
115703e
58d3a8d
23ef488
9dc4645
6b47421
e7bc55c
8606171
f521847
5edd47b
cf93ab4
5442a69
4f0baa2
c1680ba
f933a55
7c9c36e
e3a45eb
786c61b
c4fa0e1
b7ab5fb
26e33d8
8df08a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
def match_word_pattern(pattern :str, input_string: str) -> bool: | ||
""" | ||
Determine if a given pattern matches a string using backtracking. | ||
|
||
pattern: The pattern to match. | ||
input_string: The string to match against the pattern. | ||
return: True if the pattern matches the string, False otherwise. | ||
|
||
>>> match_word_pattern("aba", "GraphTreesGraph") | ||
True | ||
|
||
>>> match_word_pattern("xyx", "PythonRubyPython") | ||
True | ||
|
||
>>> match_word_pattern("GG", "PythonJavaPython") | ||
False | ||
""" | ||
def backtrack(pattern_index, str_index): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
if pattern_index == len(pattern) and str_index == len(input_string): | ||
return True | ||
if pattern_index == len(pattern) or str_index == len(input_string): | ||
return False | ||
char = pattern[pattern_index] | ||
if char in pattern_map: | ||
mapped_str = pattern_map[char] | ||
if input_string.startswith(mapped_str, str_index): | ||
return backtrack(pattern_index + 1, str_index + len(mapped_str)) | ||
else: | ||
return False | ||
for end in range(str_index + 1, len(input_string) + 1): | ||
substr = input_string[str_index:end] | ||
if substr in str_map: | ||
continue | ||
pattern_map[char] = substr | ||
str_map[substr] = char | ||
if backtrack(pattern_index + 1, end): | ||
return True | ||
del pattern_map[char] | ||
del str_map[substr] | ||
return False | ||
pattern_map = {} | ||
str_map = {} | ||
return backtrack(0, 0) | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
backtracking/match_word_pattern.py
, please provide doctest for the functionbacktrack
Please provide return type hint for the function:
backtrack
. If the function does not return a value, please provide the type hint as:def function() -> None:
Please provide type hint for the parameter:
pattern_index
Please provide type hint for the parameter:
str_index