**一. 题目描述**
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.
**二. 题目分析**
此题可使用二维动态规划来解决,下表给出了直观的匹配过程:
![](https://box.kancloud.cn/2016-01-05_568bb5f07f44b.jpg)
设某一格的状态为`k[i][j]`,表示`s1[i]`或`s2[j]`,与`s3[i+j]`的匹配结果。`s3`可与`s1`和`s2`相匹配时,可分为以下两种情况:
如果`s1` 的最后一个字符等于`s3` 的最后一个字符,则`k[i][j]=k[i-1][j]`;
如果`s2` 的最后一个字符等于`s3` 的最后一个字符,则`k[i][j]=k[i][j-1]`。
因此状态转移方程如下:
`f[i][j] = (s1[i - 1] == s3 [i + j - 1] && f[i - 1][j]) || (s2[j - 1] == s3 [i + j - 1] && f[i][j - 1]);`
**三. 示例代码**
~~~
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution
{
public:
bool isInterleave(string s1, string s2, string s3)
{
if (s3.size() != s1.size() + s2.size())
return false;
if (s3[0] != s1[0] && s3[0] != s2[0])
return false;
vector<vector<bool> > k(s1.size() + 1, vector<bool>(s2.size() + 1, false));
k[0][0] = true;
// 边界设置
for (size_t i = 1; i <= s1.size(); ++i)
k[i][0] = (s1[i - 1] == s3[i - 1]) && k[i - 1][0];
for (size_t j = 1; j <= s2.size(); ++j)
k[0][j] = (s2[j - 1] == s3[j - 1]) && k[0][j - 1];
for (size_t i = 1; i <= s1.size(); ++i)
{
for (size_t j = 1; j <= s2.size(); ++j)
{
k[i][j] = ((s1[i - 1] == s3[i + j - 1]) && k[i - 1][j]) ||
((s2[j - 1] == s3[i + j - 1]) && k[i][j - 1]);
}
}
return k[s1.size()][s2.size()];
}
};
~~~
![](https://box.kancloud.cn/2016-01-05_568bb5f08c5ed.jpg)
![](https://box.kancloud.cn/2016-01-05_568bb5f0a43fe.jpg)
**四. 小结**
编程时要注意边界条件的问题和数组的下标问题。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching