forked from yingl/LintCodeInPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterleaving_string.py
More file actions
Latest commit
23 lines (21 loc) · 994 Bytes
/
Copy pathinterleaving_string.py
File metadata and controls
23 lines (21 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding: utf-8 -*-
classSolution:
"""
@params s1, s2, s3: Three strings as description.
@return: return True if s3 is formed by the interleaving of
s1 and s2 or False if not.
@hint: you can use [[True] * m for i in range (n)] to allocate a n*m matrix.
"""
defisInterleave(self, s1, s2, s3):
# write your code here
returnself._isInterleave(s3, 0, s1, 0, s2, 0)
def_isInterleave(self, s3, s3_start, s1, s1_start, s2, s2_start):
if (s3_start==len(s3)) and (s1_start==len(s1)) and (s2_start==len(s2)):
returnTrue
if (s1_start<len(s1)) and (s3[s3_start] ==s1[s1_start]):
ifself._isInterleave(s3, s3_start+1, s1, s1_start+1, s2, s2_start):
returnTrue
if (s2_start<len(s2)) and (s3[s3_start] ==s2[s2_start]):
ifself._isInterleave(s3, s3_start+1, s1, s1_start, s2, s2_start+1):
returnTrue
returnFalse