-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotating_substring.cpp
More file actions
57 lines (57 loc) · 1.14 KB
/
Copy pathrotating_substring.cpp
File metadata and controls
57 lines (57 loc) · 1.14 KB
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
// solution is not correct and also in O(n3) so convert the following solution in dp where complexity lies in O(n2)
#include<bits/stdc++.h>
using namespace std;
string s,t;
int rotate(int a, int b)
{
char x=s[b];
for(int i=b;i>a;i--)
{
s[i]=s[i-1];
}
s[a]=x;
return 0;
}
int main()
{
int z;
cin>>z;
while(z--)
{
int n;
cin>>n;
cin>>s>>t;
int x[26]={0};
for(int i=0;i<n;i++)
{
x[s[i]-97]++;
x[t[i]-97]--;
}
int flag=0;
for(int i=0;i<26;i++)
if(x[i])
flag++;
if(flag)
{
cout<<-1<<endl;
continue;
}
int count=0;
for(int i=n-1;i>=0;i--)
{
if(t[i]==s[i])
continue;
for(int j=0;j<i;j++)
{
if(t[i]==s[j])
{
count++;
rotate(i,j);
break;
}
}
}
cout<<count<<endl;
}
return 0;
}