forked from imsushant12/GFG-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
String_is_a_valid_shuffle_of_2_strings_or_not.cpp
72 lines (59 loc) · 1.16 KB
/
String_is_a_valid_shuffle_of_2_strings_or_not.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
/*
Example:
--------
1XY2 is a valid shuffle of XY and 12
Y12X is not a valid shuffle of XY and 12
*/
#include <bits/stdc++.h>
using namespace std;
int shuffleString(string s1 , string s2 , string res)
{
int l1 = s1.length();
int l2 = s2.length();
int l3 = res.length();
// i --> first string ; j --> second string ; k --> third string
int i=0 , j=0 , k=0;
bool flag = true;
if(l1+l2 != l3)
return 0;
else
{
while(k < l3)
{
if(s1[i] == res[k])
{
i++;
k++;
}
else if(s2[j] == res[k])
{
j++;
k++;
}
else
{
flag = false;
break;
}
}
}
if(flag == true)
return 1;
else
return 0;
}
int main()
{
string s1,s2,res;
cout<<"Enter the first string : ";
cin>>s1;
cout<<"Enter the second string : ";
cin>>s2;
cout<<"Enter the resultant string : ";
cin>>res;
if(shuffleString(s1 , s2 , res))
cout<<"YES";
else
cout<<"NO";
return 0;
}