Skip to content

Commit eaabdc1

Browse files
Create Strings of Impurity.cpp
1 parent f6b3e3b commit eaabdc1

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
int main()
8+
{
9+
string S, T;
10+
cin >> S >> T;
11+
12+
const int NO_OF_ALPHABETS = 26;
13+
vector <vector <int> > locations(NO_OF_ALPHABETS);
14+
for(int i = 0; i < S.size(); i++)
15+
{
16+
locations[S[i] - 'a'].push_back(i);
17+
}
18+
19+
int no_of_concatenations = 1, matched_prefix = -1;
20+
for(int i = 0; i < T.size(); i++)
21+
{
22+
if(locations[T[i] - 'a'].size() == 0)
23+
{
24+
cout << "-1\n";
25+
26+
return 0;
27+
}
28+
29+
auto it = upper_bound(locations[T[i] - 'a'].begin(), locations[T[i] - 'a'].end(), matched_prefix);
30+
31+
if(it == locations[T[i] - 'a'].end())
32+
{
33+
no_of_concatenations++;
34+
35+
matched_prefix = locations[T[i] - 'a'][0];
36+
}
37+
else
38+
{
39+
matched_prefix = *it;
40+
}
41+
}
42+
43+
long long final_length = (no_of_concatenations - 1)*1LL*S.size() + (matched_prefix + 1);
44+
cout << final_length << "\n";
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)