forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interleaving_String.cpp
68 lines (56 loc) · 1.31 KB
/
Interleaving_String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Oct 24, 2012
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Problem:
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.
Note:
DP.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int n = s1.length();
int m = s2.length();
bool f[n + 1][m + 1];
if (n + m != s3.length()) {
return false;
}
f[0][0] = true;
for (int i = 1; i <= n; i++) {
f[i][0] = s1.substr(0, i).compare(s3.substr(0, i)) == 0;
}
for (int j = 1; j <= m; j++) {
f[0][j] = s2.substr(0, j).compare(s3.substr(0, j)) == 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = false;
if (s1[i - 1] == s3[i + j - 1] && f[i - 1][j]) {
f[i][j] = true;
}
if (s2[j - 1] == s3[i + j - 1] && f[i][j - 1]) {
f[i][j] = true;
}
}
}
return f[n][m];
}
};