zoukankan      html  css  js  c++  java
  • 97. Interleaving String

    Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

    Example 1:

    Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
    Output: true
    

    Example 2:

    Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
    Output: false
    class Solution {
        public boolean isInterleave(String s1, String s2, String s3) {
            int n1 = s1.length(), n2 = s2.length();
            if(n1 + n2 != s3.length()) return false;
            boolean[][] dp = new boolean[n1+1][n2+1];
            for(int i = 0; i <= n1; i++){
                for(int j = 0; j <= n2; j++){
                    if(i == 0 && j == 0) dp[i][j] = true;
                    else if(i == 0){
                        dp[i][j] = dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1);
                    }
                    else if(j == 0){
                        dp[i][j] = dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1);
                    }
                    else{
                        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[n1][n2];
        }
    }

     

     https://www.cnblogs.com/grandyang/p/4298664.html

  • 相关阅读:
    Linux常用操作命令总结
    Centos7安装FastDFS教程
    Permutation Sequence
    Next Permutation
    Remove Element
    4Sum
    3Sum Closest
    3Sum
    Longest Consecutive Sequence
    Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/11658025.html
Copyright © 2011-2022 走看看