• Interleaving String——Leetcode


    Given s1s2s3, 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.

    题目大意:给定三个字符串,s1,s2,s3,问s3是否由s1和s2交叉构成。

    解题思路:递归超时,重复计算太多,加上cache可能可以。

    说一下dp的方法,研究了半天才看明白,用一个二维布尔数组dp[i][j]表示从s1中取前i位,从s2中取前j位构成s3的前i+j位是否可以。

    那么dp[i][j]可能从dp[i-1][j]或dp[i][j-1]而来:

      如果从dp[i-1][j]来,那么dp[i-1][j]应该为true,并且s1.charAt(i-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;

      如果从dp[i][j-1]来,那么dp[i][j-1]应该为true,并且s2.charAt(j-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;

    初始化,dp[0][0]=true表示两个空字符串构成一个空字符串为true。

    另外,从s1或s2开始构成s3的前n或m个字符也是初始条件。

    举个列子,注意左上角那一对箭头指向的格子dp[1][1], 表示s1取第1位a, s2取第1位d,是否能组成s3的前两位aa

    从dp[0][1] 往下的箭头表示,s1目前取了0位,s2目前取了1位,我们添加s1的第1位,看看它是不是等于s3的第2位,( i + j 位)

    从dp[1][0] 往右的箭头表示,s1目前取了1位,s2目前取了0位,我们添加s2的第1位,看看它是不是等于s3的第2位,( i + j 位)

      public static boolean isInterleave(String s1, String s2, String s3) {
            if (s3.length() != s1.length() + s2.length()) {
                return false;
            }
            boolean[][] dp = new boolean[s1.length() + 1][s2.length() + 1];
            dp[0][0] = true;
            for (int i = 0; i < s1.length(); i++) {
                if (s1.charAt(i) == s3.charAt(i)) {
                    dp[i + 1][0] = dp[i][0];
                }
            }
            for (int i = 0; i < s2.length(); i++) {
                if (s2.charAt(i) == s3.charAt(i)) {
                    dp[0][i + 1] = dp[0][i];
                }
            }
            for (int i = 1; i < s1.length() + 1; i++) {
                for (int j = 1; j < s2.length() + 1; j++) {
                    dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
                }
            }
            return dp[s1.length()][s2.length()];
        }

     参考:http://blog.csdn.net/u011095253/article/details/9248073

  • 相关阅读:
    重定向 重写
    php 安装 event 和 libevent 扩展
    curl 和 tcpdump
    yum 升级php版本
    shell 教程
    shell脚本 inotify + rsync 同步脚本
    nodesj中 中间件express-session的理解
    node.js中express-session配置项详解
    我对面向对象的理解
    BootstrapValidator
  • 原文地址:https://www.cnblogs.com/aboutblank/p/4892470.html
Copyright © 2020-2023  润新知