【LeetCode OJ】Interleaving String
Problem Link:
http://oj.leetcode.com/problems/interleaving-string/
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc"
,
s2 = "dbbca"
,
When s3 = "aadbbcbcac"
, return true.
When s3 = "aadbbbaccc"
, return false.
This problem can solved by using DFS on a binary tree. The tree node has three integers (p1,p2,p3), which denotes that s3[0..p3-1] is the interleaving of s1[0..p1-1] and s2[0..p2-1]. For each node, there are following choices:
- If p3 == len(s3), it follows that s3 is the interleaving of s1 and s2 (we need assume that len(s3) = len(s1)+len(s2)), return True
- If p1 < len(s1) and s1[p1] == s3[p3], then s3[0..p3] could be the interleaving of s1[0..p1] and s2[0..p2-1], so we can go deeper to the node (p1+1, p2, p3+1);
- If p2 < len(s2) and s2[p2] == s3[p3], then s3[0..p3] could be the interleaving of s1[0..p1-1] and s2[0..p2], so we can go deeper to the node (p1, p2+1, p3+1);
- Otherwise, we cannot go deeper and this path will be terminated.
If we check all possible paths using DFS, and there is no successful path, we would return False and terminate the program. The python code should be as follows.
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
n1 = len(s1)
n2 = len(s2)
n3 = len(s3)
if n1 + n2 != n3:
return False
# DFS
q = []
q.append( (0,0,0) ) # checked length for s1, s2, s3
while q:
l1, l2, l3 = q.pop()
# If checked length of s3 equals to length of s3, then return True
if l3 == n3:
return True
# If availabe char in s1:
if l1 < n1 and s1[l1] == s3[l3]:
q.append((l1+1,l2,l3+1))
# If available char in s2:
if l2 < n2 and s2[l2] == s3[l3]:
q.append((l1,l2+1,l3+1))
return False
I used build-in structure list implementing the Queue structure. However, the leetcode judging system returns timeout for this DFS implementation. I think it will be worse if you use recursive function instead of queue to carry out the DFS.
So I have to solve it in a more efficient way, I choose DP. I define a boolean 2D array A[0..n1][0..n2] to denote if s3[0:i+j] is the interleaving of s1[0:i] and s2[0:j]. The recursive formula for this DP solution is:
A[0][0] = True, since "" is the interleaving of "" and ""
A[0][j] = True, if s2[0:j] == s3[0:j], which means s3 is the interleaving of "" and s2 if s2 == s3
A[i][0] = True, if s1[0:i] == s3[0:i], which means s3 is the interleaving of s1 and "" if s1 == s3
A[i][j] = True if 1) A[i-1][j] = True and s1[i-1] == s3[i+j-1]; or 2) A[i][j-1] = True and s2[j-1] == s3[i+j-1], for 0<i<=n1, 0<j<=n2
A[][] is (n1+1)*(n2+1) array where A[i][j] means s3[0:i+j] is the interleaving of s1[0:i] and s2[0:j]. We can fill the A-table in a bottom-up way and just return A[n1][n2] to see if s3 is the interleaving of s1 and s2. The python code is as follows, which accepted by the leetcode OJ system.
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
# A[i][j] means s1[0:i] and s2[0:j] is interleaves of s3[0:i+j]
# A[0][0] = True, since "" and "" are interleaves of ""
# A[0][j] = True, if s2[0:j] == s3[0:j]
# A[i][0] = True, if s1[0:i] == s3[0:j]
# A[i][j] = True, if any of following is true:
# 1) A[i-1][j] = True and s1[i-1] == s3[i+j-1]
# 2) A[i][j-1] = True and s2[j-1] == s3[i+j-1]
n1 = len(s1)
n2 = len(s2)
n3 = len(s3)
if n1 + n2 != n3:
return False
# Initialization
A = []
for _ in xrange(n1+1):
A.append([False]*(n2+1))
# Boundary conditions
A[0][0] = True
for j in xrange(n2):
if s2[j] == s3[j]:
A[0][j+1] = True
for i in xrange(n1):
if s1[i] == s3[i]:
A[i+1][0] = True
# Fill the table
for x in xrange(n1):
for y in xrange(n2):
i = x+1
j = y+1
if (A[i-1][j] and s1[i-1] == s3[i+j-1]) or (A[i][j-1] and s2[j-1] == s3[i+j-1]):
A[i][j] = True
return A[n1][n2]
【LeetCode OJ】Interleaving String的更多相关文章
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Word Ladder I
Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode OJ】Word Break
Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...
随机推荐
- 安装SQL Server 2014
一:下载SQL SERVER 2014 https://www.microsoft.com/zh-cn/server-cloud/products/sql-server-editions/sql-se ...
- NSException
NSException是什么? 最熟悉的陌生人,这是我对NSException的概述,为什么这么说呢?其实很多开发者接触到NSException的频率非常频繁,但很多人都不知道什么是NSExcepti ...
- iOS 中对 HTTPS 证书链的验证
这篇文章是我一边学习证书验证一边记录的内容,稍微整理了下,共扯了三部分内容: HTTPS 简要原理: 数字证书的内容.生成及验证: iOS 上对证书链的验证. HTTPS 概要 HTTPS 是运行在 ...
- 【转载】JSP中文乱码问题
原作者http://www.cnblogs.com/xing901022/p/4354529.html 阅读目录 之前总是碰到JSP页面乱码的问题,每次都是现在网上搜,然后胡乱改,改完也不明白原因. ...
- grease monkey setTimeout
在grease monkey中要使用如下方法进行setTimeout var f = function(){alert(1); setTimeout(f,100); } var inst=setTim ...
- 必须关注的25位知名JavaScript开发者
必须关注的25位知名JavaScript开发者 发表于2012-08-07 17:30| 16215次阅读| 来源Crossrider Blog| 46 条评论| 作者Crossrider Blog ...
- comboBox的多选框之疑难杂症——逗号篇
提笔写正文之前,首先要再次提醒一下自己,因为总是记不住,以至大神同事们都开始用“嫌弃”的眼光看自己了——遇到问题,自己去解决,没有什么问题是解决不掉的,不要在没认真努力思考之前就去麻烦大神同事,切记切 ...
- Entity Framework系列
这个系列主要记录学习EF的过程和碰到的问题以及解决问题的方法. EF中的那些批量操作 EF的Model First
- springmvc 配置直接访问页面
<mvc:view-controller path="/" view-name="/home"/> 在mvc中配置,访问路径就可以了
- zatree第三方插件
Zabbix安装第三方插件zatree2.4.5 1.下载zatree第三方插件https://github.com/spide4k/zatree.git 2.检查PHP环境需要支持php-xml.p ...