Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def solve(string, regex):
- left_regex, right_regex = regex.split("*")
- left = right = -1
- for idx in range(len(string)):
- i = 0
- ptr = idx
- while i < len(left_regex) and ptr < len(string) and string[ptr] == left_regex[i]:
- ptr += 1
- i += 1
- if i == len(left_regex): # matched left regex
- left = idx
- break
- for idx in range(len(string) - 1, -1, -1):
- i = len(right_regex) - 1
- ptr = idx
- while i >= 0 and ptr > left and string[ptr] == right_regex[i]:
- ptr -= 1
- i -= 1
- if i == -1: # matched right regex
- right = idx
- break
- if -1 in (left, right):
- return -1
- return right - left + 1
- def main():
- tc = [("abcmnobcd", "abc*bcd"), ("abc", "a*"), ("abcd", "a*e")]
- for string, regex in tc:
- result = solve(string, regex)
- print(result)
- main()
Advertisement
Add Comment
Please, Sign In to add comment