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
23 lines (21 loc) · 994 Bytes
/
interleaving_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 -*-
class Solution:
"""
@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.
"""
def isInterleave(self, s1, s2, s3):
# write your code here
return self._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)):
return True
if (s1_start < len(s1)) and (s3[s3_start] == s1[s1_start]):
if self._isInterleave(s3, s3_start + 1, s1, s1_start + 1, s2, s2_start):
return True
if (s2_start < len(s2)) and (s3[s3_start] == s2[s2_start]):
if self._isInterleave(s3, s3_start + 1, s1, s1_start, s2, s2_start + 1):
return True
return False