FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-1/src/interleavingString/interleavingString.cpp at master · daction/leetcode-1 · GitHub
daction
/
leetcode-1
Public
forked from
haoel/leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-1
/
src
/
interleavingString
/
interleavingString.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
146 lines (114 loc) · 3.8 KB
Breadcrumbs
leetcode-1
/
src
/
interleavingString
/
interleavingString.cpp
Copy path
File metadata and controls
146 lines (114 loc) · 3.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
Source : https://oj.leetcode.com/problems/interleaving-string/
//
Author : Hao Chen
//
Date : 2014-08-27
/*
*********************************************************************************
*
* 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
<
iostream
>
#
include
<
string
>
#
include
<
vector
>
using
namespace
std
;
/*
Considering:
s1 = a1, a2 ........a(i-1), ai
s2 = b1, b2, .......b(j-1), bj
s3 = c1, c3, .......c(i+j-1), c(i+j)
Defined
match[i][j] means s1[0..i] and s2[0..j] is matched S3[0..i+j]
So, if ai == c(i+j), then match[i][j] = match[i-1][j], which means
s1 = a1, a2 ........a(i-1)
s2 = b1, b2, .......b(j-1), bj
s3 = c1, c3, .......c(i+j-1)
Same, if bj = c(i+j), then match[i][j] = match[i][j-1];
Formula:
Match[i][j] =
(s3[i+j-1] == s1[i]) && match[i-1][j] ||
(s3[i+j-1] == s2[j]) && match[i][j-1]
Initialization:
i=0 && j=0, match[0][0] = true;
i=0, s3[j] == s2[j], match[0][j] |= match[0][j-1]
s3[j] != s2[j], match[0][j] = false;
j=0, s3[i] == s1[i], match[i][0] |= match[i-1][0]
s3[i] != s1[i], Match[i][0] = false;
*/
//
Dynamic Programming
bool
isInterleave
(string s1, string s2, string s3) {
if
(s1.
size
() + s2.
size
() != s3.
size
()) {
return
false
;
}
vector< vector<
int
> >
match
(s1.
size
()+
1
, vector<
int
>(s2.
size
()+
1
,
false
) );
match[
0
][
0
] =
true
;
for
(
int
i=
1
; i<=s1.
size
(); i++) {
if
(s1[i-
1
] == s3[i-
1
] ) {
match[i][
0
] =
true
;
}
else
{
break
;
}
}
for
(
int
i=
1
; i<=s2.
size
(); i++) {
if
(s2[i-
1
] == s3[i-
1
] ) {
match[
0
][i] =
true
;
}
else
{
break
;
}
}
for
(
int
i=
1
; i<=s1.
size
(); i++) {
for
(
int
j=
1
; j<=s2.
size
(); j++) {
if
(s1[i-
1
] == s3[i+j-
1
]) {
match[i][j] = match[i-
1
][j] || match[i][j];
}
if
(s2[j-
1
] == s3[i+j-
1
]) {
match[i][j] = match[i][j-
1
] || match[i][j];
}
}
}
return
match[s1.
size
()][s2.
size
()];
}
//
Time Limit Exceeded
bool
isInterleave_dfs
(string s1, string s2, string s3) {
if
(s1.
size
() + s2.
size
() != s3.
size
()) {
return
false
;
}
const
char
*p1 = s1.
c_str
(), *p2 = s2.
c_str
(), *p3 = s3.
c_str
();
for
(; *p3 !=
'
\0
'
; p3++){
if
(*p3 == *p1 && *p3!=*p2) {
p1++;
}
else
if
( *p3 == *p2 && *p3 != *p1) {
p2++;
}
else
if
(*p3==*p1 && *p3 ==*p2) {
if
(
isInterleave
(p1+
1
, p2, p3+
1
) ==
false
){
return
isInterleave
(p1, p2+
1
, p3+
1
);
}
return
true
;
}
else
{
return
false
;
}
}
return
( *p1 ==
'
\0
'
&& *p2 ==
'
\0
'
&& *p3 ==
'
\0
'
);
}
#
define
TEST
(
s1, s2, s3
) cout << s1 <<
"
,
"
<< s2 <<
"
=
"
<< s3 <<
"
:
"
<< isInterleave(s1,s2,s3) << endl;
int
main
(
int
argc,
char
**argv)
{
string s1=
"
aabcc
"
, s2 =
"
dbbca
"
;
TEST
(s1,s2,
"
aadbbcbcac
"
);
TEST
(s1,s2,
"
aadbbbaccc
"
);
s1=
"
c
"
; s2=
"
ca
"
;
TEST
(s1, s2,
"
cca
"
);
TEST
(s1, s2,
"
cac
"
);
s1 =
"
bbbbbabbbbabaababaaaabbababbaaabbabbaaabaaaaababbbababbbbbabbbbababbabaabababbbaabababababbbaaababaa
"
;
s2 =
"
babaaaabbababbbabbbbaabaabbaabbbbaabaaabaababaaaabaaabbaaabaaaabaabaabbbbbbbbbbbabaaabbababbabbabaab
"
;
string s3 =
"
babbbabbbaaabbababbbbababaabbabaabaaabbbbabbbaaabbbaaaaabbbbaabbaaabababbaaaaaabababbababaababbababbbababbbbaaaabaabbabbaaaaabbabbaaaabbbaabaaabaababaababbaaabbbbbabbbbaabbabaabbbbabaaabbababbabbabbab
"
;
TEST
(s1, s2, s3);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL