-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterleaving_String.cpp
More file actions
79 lines (68 loc) · 1.8 KB
/
Interleaving_String.cpp
File metadata and controls
79 lines (68 loc) · 1.8 KB
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
69
70
71
72
73
74
75
76
77
78
// Source : https://oj.leetcode.com/problems/interleaving-string/
// Author : zheng yi xiong
// Date : 2014-12-26
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int s1_len = s1.length();
int s2_len = s2.length();
int s3_len = s3.length();
if (s1_len + s2_len != s3_len)
{
return false;
}
vector<vector<bool> > used_route(s1_len + 1, vector<bool>(s2_len + 1, 0));
used_route[0][0] = true;
for (int s1_pos = 1; s1_pos <= s1_len; ++s1_pos)
{
if (s1[s1_pos - 1] == s3[s1_pos - 1])
{
used_route[s1_pos][0] = true;
}
}
for (int s2_pos = 1; s2_pos <= s2_len; ++s2_pos)
{
if (s2[s2_pos - 1] == s3[s2_pos - 1])
{
used_route[0][s2_pos] = true;
}
}
for (int s1_pos = 1; s1_pos <= s1_len; ++s1_pos)
{
for (int s2_pos = 1; s2_pos <= s2_len; ++s2_pos)
{
if ( (used_route[s1_pos - 1][s2_pos] && s1[s1_pos - 1] == s3[s1_pos + s2_pos - 1]) ||
(used_route[s1_pos][s2_pos - 1] && s2[s2_pos - 1] == s3[s1_pos + s2_pos - 1]) )
{
used_route[s1_pos][s2_pos] = true;
}
}
}
return used_route[s1_len][s2_len];
}
};
int _tmain(int argc, _TCHAR* argv[])
{
string s1 = "aabaac";
string s2 = "aadaaeaaf";
string s3 = "aadaaeaabaafaac";/*"aadbbbaccc"*/;
Solution so;
bool bIn = so.isInterleave(s1, s2, s3);
return 0;
}