Skip to content

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

Merged
merged 33 commits into from
Oct 5, 2023
Merged
Changes from 23 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4e9df4a
Fix: Issue 9588
haxkd Oct 4, 2023
1fab302
Fix: Issue 9588
haxkd Oct 4, 2023
8704157
Merge branch 'TheAlgorithms:master' into master
haxkd Oct 4, 2023
143764d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
045652d
Fix: Issue 9588
haxkd Oct 4, 2023
b5fb9f5
Merge branch 'master' of https://github.com/haxkd/Python
haxkd Oct 4, 2023
95161a0
Fix: Issue #9588
haxkd Oct 4, 2023
155601c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
376cb60
Fix: Issue #9588
haxkd Oct 4, 2023
6a424b3
Fix: Issue #9588
haxkd Oct 4, 2023
a878e4b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
2b096d9
Merge branch 'TheAlgorithms:master' into master
haxkd Oct 5, 2023
115703e
Fix: Issue #9588
haxkd Oct 5, 2023
58d3a8d
Fix: Issue #9588
haxkd Oct 5, 2023
23ef488
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
9dc4645
Merge branch 'TheAlgorithms:master' into master
haxkd Oct 5, 2023
6b47421
fix: issue #9793
haxkd Oct 5, 2023
e7bc55c
fix: issue #9793
haxkd Oct 5, 2023
8606171
fix: issue #9588
haxkd Oct 5, 2023
f521847
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
5edd47b
Merge branch 'TheAlgorithms:master' into master
haxkd Oct 5, 2023
cf93ab4
Merge branch 'TheAlgorithms:master' into master
haxkd Oct 5, 2023
5442a69
fix: issue #9844
haxkd Oct 5, 2023
4f0baa2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
c1680ba
fix: issue #9844
haxkd Oct 5, 2023
f933a55
fix: issue #9844
haxkd Oct 5, 2023
7c9c36e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
e3a45eb
fix: issue #9844
haxkd Oct 5, 2023
786c61b
fix: issue #9844
haxkd Oct 5, 2023
c4fa0e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
b7ab5fb
Merge branch 'TheAlgorithms:master' into master
haxkd Oct 5, 2023
26e33d8
fix: issue #9844
haxkd Oct 5, 2023
8df08a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
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
54 changes: 54 additions & 0 deletions backtracking/match_word_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
def matchWordPattern(pattern :str, input_string: str) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: matchWordPattern

"""
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.

>>> matchWordPattern("aba", "GraphTreesGraph")
True

>>> matchWordPattern("xyx", "PythonRubyPython")
True

>>> matchWordPattern("GG", "PythonJavaPython")
False
"""
def backtrack(pattern_index, str_index):

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 function backtrack

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

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 function backtrack

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

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 function backtrack

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

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 function backtrack

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

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()