-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterleavingStrings.cpp
47 lines (38 loc) · 1.12 KB
/
InterleavingStrings.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
// Check if two string are interleaving of third https://www.geeksforgeeks.org/check-whether-a-given-string-is-an-interleaving-of-two-other-given-strings/
#include <iostream>
#include <string>
bool isInterleaving(const std::string& a, const std::string& b, const std::string& c)
{
if (a.size() + b.size() != c.size()) {
return false;
}
int indexA = 0;
int indexB = 0;
int indexC = 0;
while (indexC < c.size()) {
if (indexA < a.size() && a[indexA] == c[indexC]) {
indexA++;
} else if (indexB < b.size() && b[indexB] == c[indexC]) {
indexB++;
} else {
return false;
}
indexC++;
}
return true;
}
void test(const std::string& a, const std::string& b, const std::string& c)
{
std::cout << c << " interleaved of " << a << " & " << b << " : ";
std::cout << std::boolalpha << isInterleaving(a, b, c) << "\n";
}
int main()
{
test("AB", "CD", "ACBG");
test("AB", "CD", "ACBD");
test("A", "CD", "ACBD");
test("AB", "C", "ACBD");
test("AB", "CD", "ACBDE");
test("AAB", "AAC", "AACAAB");
return 0;
}