[leetcode-97-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.

思路:

“Here is some explanation:

DP table represents if s3 is interleaving at (i+j)th position when s1 is at ith position, and s2 is at jth position. 0th position means empty string.

So if both s1 and s2 is currently empty, s3 is empty too, and it is considered interleaving. If only s1 is empty, then if previous s2 position is interleaving and current s2 position char is equal to s3 current position char, it is considered interleaving. similar idea applies to when s2 is empty. when both s1 and s2 is not empty, then if we arrive i, j from i-1, j, then if i-1,j is already interleaving and i and current s3 position equal, it s interleaving. If we arrive i,j from i, j-1, then if i, j-1 is already interleaving and j and current s3 position equal. it is interleaving.

bool isInterleave(string s1, string s2, string s3)
 {
    if(s3.length() != s1.length() + s2.length())return false;
    bool table[s1.length()+1][s2.length()+1];

    for(int i=0;i<s1.length()+1;i++)
    {
      for(int j=0;j<s2.length()+1;j++)
      {
    if(i==0&& j==0)table[i][j] = true;
    else if(i ==0) table[i][j] = (table[i][j-1] &&  s2[j-1] == s3[i+j-1] );
    else if(j == 0)table[i][j] = (table[i-1][j] && s1[i-1] == s3[i +j-1]);
    else     table[i][j] =  (table[i-1][j] && s1[i-1] == s3[i +j -1]) || (table[i][j-1] &&  s2[j-1] == s3[i+j-1] ) ;
      }
  }
  return table[s1.length()][s2.length()];
 }

参考:

https://discuss.leetcode.com/topic/3532/my-dp-solution-in-c

时间: 2024-08-26 17:27:34

[leetcode-97-Interleaving String]的相关文章

leetCode 97.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. 思路:此题刚开始有点想当然了

【leetcode】Interleaving String

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

Leetcode dp Interleaving String

Interleaving String Total Accepted: 15409 Total Submissions: 79580My Submissions 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&q

【LeetCode】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. [解析] 题意:有

[LeetCode][Java] 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. 题意: 给定s1, 

[leedcode 97] 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. public class Solut

Java for LeetCode 097 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. 解题思路: DP问题,JAV

97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串

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. class Solution { p

97. 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. public bool IsInte

97. Interleaving String Add to List

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. 解题思路:很经典的一道DP题.tab