-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterleaveingString.cpp
144 lines (109 loc) · 3.37 KB
/
InterleaveingString.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
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
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
// bug
if (len1 + len2 != len3)
return false;
vector< vector<int> > M = vector< vector<int> >(len1+1, vector<int>(len2+1, 0) );
if (len1 == 0 && len2 == 0)
{
if (len3 == 0) return true;
else return false;
}
if (len1 == 0 )
{
if (s3 == s2) return true;
else return false;
}
if (len2 == 0)
{
if (s1 == s3) return true;
else return false;
}
for(int i=1; i<=len1; i++)
{
string s(s1, 0 , i );
string sub_s3 (s3, 0, i);
if (s == sub_s3)
M[i][0] = 1;
}
for(int i=1; i<=len2; i++)
{
string s(s2, 0 , i );
string sub_s3 (s3, 0, i);
if (s == sub_s3)
M[0][i] = 1;
}
for(int i=1; i<=len1; i++)
{
for(int j=1; j<=len2; j++)
{
// bug, s index error
if ( s3[i+j-1] == s2[j-1] && M[i][j-1] == 1)
M[i][j] = 1;
// missing this logic
if (s3[i+j-1] == s1[i-1] && M[i-1][j] == 1)
M[i][j] = 1;
}
}
if( M[len1][len2] == 1) return true;
else return false;
}
};
/*
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (s3.length() == 0 && s1.length() == 0 && s2.length() == 0)
return true;
if (s1.length() == 0)
{
if (s2 == s3)
return true;
else
return false;
}
if (s2.length() == 0)
{
if (s1 == s3)
return true;
else
return false;
}
if (s3.length() == 0 && (s1.length()>0 || s2.length() > 0) )
return false;
if (s3.length() != s1.length() + s2.length() )
return false;
char c = s3[0];
bool result1 = false;
bool result2 = false;
if (s1.length() > 0 && c == s1[0] )
{
string new_s1(s1, 1);
//new_s1.assign(s1, 1);
string new_s3(s3, 1);
//new_s3.assign;
result1 = isInterleave(new_s1 , s2, new_s3 );
if (result1 == true)
return true;
}
if (s2.length() > 0 && c == s2[0] )
{
string new_s2(s2, 1);
//new_s2.assign;
string new_s3(s3, 1);
//new_s3.assign;
result2 = isInterleave(s1, new_s2, new_s3);
if (result2 == true)
return true;
}
return false;
}
}; */