-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterleaving-string.py
More file actions
74 lines (72 loc) · 2.64 KB
/
Copy pathinterleaving-string.py
File metadata and controls
74 lines (72 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 15 19:39:33 2022
@author: patha
"""
class Solution(object):
def isInterleave(self, s1, s2, s3):
"""
:type s1: str
:type s2: str
:type s3: str
:rtype: bool
"""
def _int_helper_(arg1, arg2, arg3, choice):
# print(arg1, arg2, arg3, choice)
nonlocal mem_dict
if (arg1, arg2, arg3, choice) in mem_dict.keys():
return mem_dict[(arg1, arg2, arg3, choice)]
if choice == 1:
if not arg1:
if not arg3:
return True
return False
elif arg1 == arg3:
return True
arg1_overlap = 0
for i in range(len(arg1)):
if arg1[i] == arg3[i]:
arg1_overlap += 1
else:
break
if arg1_overlap == 0:
return False
retval = False
for i in range(1, arg1_overlap + 1):
if (arg1[i:], arg2, arg3[i:], 2) not in mem_dict.keys():
mem_dict[(arg1[i:], arg2, arg3[i:], 2)] = _int_helper_(arg1[i:], arg2, arg3[i:], 2)
retval = retval or mem_dict[(arg1[i:], arg2, arg3[i:], 2)]
if retval:
return retval
return retval
else:
if not arg2:
if not arg3:
return True
return False
elif arg2 == arg3:
return True
arg2_overlap = 0
for i in range(len(arg2)):
if arg2[i] == arg3[i]:
arg2_overlap += 1
else:
break
if arg2_overlap == 0:
return False
retval = False
for i in range(1, arg2_overlap + 1):
if (arg1, arg2[i:], arg3[i:], 1) not in mem_dict.keys():
mem_dict[(arg1, arg2[i:], arg3[i:], 1)] = _int_helper_(arg1, arg2[i:], arg3[i:], 1)
retval = retval or mem_dict[(arg1, arg2[i:], arg3[i:], 1)]
if retval:
return retval
return retval
mem_dict = {}
if not s1 and not s2 and not s3:
return True
elif not s1 and not s2:
return False
elif len(s3) != len(s1) + len(s2):
return False
return _int_helper_(s1, s2, s3, 1) or _int_helper_(s1, s2, s3, 2)