首页 > 其他 > 详细

Lintcode--006(交叉字符串)

时间:2016-08-24 17:27:39      阅读:223      评论:0      收藏:0      [点我收藏+]

Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2.

 

Example

For s1 = "aabcc", s2 = "dbbca"

  • When s3 = "aadbbcbcac", return true.
  • When s3 = "aadbbbaccc", return false.
Challenge

O(n2) time or better.

解题:

错解:最初,自己通过思考想出了一个解决方案。就是从字符串s3中抽取顺序抽取出字符串s2的所有字符,将s3中剩下的字符与s2进行比较,如果相等,那么就返回true,否则返回false。依照这种思路,最终通过一些修改,也只能达到66%正确,很可能是方案覆盖不完整。比如测试用例:aa,ab,abaa,就过不了。当s1,s2同时匹配的时候,这里存在一个策略问题。

正解:正解就是二维动态规划

 

Lintcode--006(交叉字符串)

原文:http://www.cnblogs.com/Allen-rg/p/5803493.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!