> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/l4dong-tai-gui-hua/interleaving-string.md).

# 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, s2的最后字母和s3比较

```
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] );
```

初始化里也要考虑前面的f

f\[0]\[j] = **f\[0]\[j - 1]** && s3.charAt(j- 1) == s2.charAt(j - 1);

```
class Solution {
    public boolean isInterleave(String s1, String s2, String s3) {
        int n = s1.length();
        int m = s2.length();
        int k = s3.length();
        if(n + m != k) 
            return false;
        boolean[][] f = new boolean[n + 1][m + 1];
        //不能在循环里初始化，是为了防止a,b的长度不足1时候。
            for(int i = 0; i <= n; i ++){                
                for(int j = 0; j <= m; j ++){
                    if(i == 0 && j == 0){
                        f[i][j] = true;
                    }else if(i == 0){
                        f[i][j] = f[i][j - 1] && s3.charAt(j- 1) == s2.charAt(j - 1);
                    }else if(j == 0){
                        f[i][j] = f[i - 1][j] && s3.charAt(i - 1) == s1.charAt(i - 1);
                    }else{
                        f[i][j] = s3.charAt(i + j - 1) == s1.charAt(i-1) && f[i - 1][j] //用i所以f里i-1
                        || s3.charAt(i + j - 1) == s2.charAt(j-1) && f[i][j - 1];
                    }            
                    }                                       
                }



        return f[n][m];
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/l4dong-tai-gui-hua/interleaving-string.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
