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 all 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
61 changes: 61 additions & 0 deletions backtracking/match_word_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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: int, str_index: int) -> bool:
"""
>>> backtrack(0, 0)
True

>>> backtrack(0, 1)
True

>>> backtrack(0, 4)
False
"""
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: dict[str, str] = {}
str_map: dict[str, str] = {}
return backtrack(0, 0)


if __name__ == "__main__":
import doctest

doctest.testmod()