Permutation – HackerEarth Problem Solution And Editorial
Problem name: Permutation.
Platform: HackerEarth.
Difficulty: Easy.
Solution method: Breadth-First Search
Click here to see the problem statement.
Solution Idea For Permutation HackerEarth Problem
It is a straight-cut Breadth-First Search (BFS) example. But you have to take a queue of strings and use a map for an easier solution.
At first, take the given input for permutation in a character variable. Then insert the characters in a string. Let’s take another string and copy the given string on that string. Then sort the new string in ascending order. This is our required string. We have to find out the minimum move to find the sorted string from the given string. So, we can run a BFS to find out the smallest number of reverse operations among the substrings to find out the required sorted string. We will run our BFS loop until we found the sorted required string. Each time we will reverse the current string elements from the first element to the second to the last element. On each operation, we will check that is the substring is visited or not. If not then we will put the map value for that string +1 from the main string. We will continue the operation until we find and label the sorted required string.
Try to solve the problem on your own. After trying your best and taking your time if you were unable to find out the solution to this problem. Look at my code.
My Code To Solve The HackerEarth Problem Permutation Using BFS:
- /* ANTHOR KUMAR DAS
- KUET ECE 2K18*/
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define sc(a) scanf(“%d”,&a)
- #define sc1(n) scanf(“%lld”,&n)
- #define tc ll tc; sc1(tc);while(tc–)
- #define sc2(b,c) scanf(“%lld%lld”,&b,&c)
- #define f(a,c,b) for(a=c;a<b;a++)
- #define pb(a) push_back(a)
- #define pf push_front
- #define p(k) printf(“%lldn”,k)
- #define pt printf
- #define en printf(“n”)
- #define br break
- #define dot(m) cout<<fixed<<setprecision(10)<<m<<endl
- #define co continue
- #define sc3(s) scanf(“%s”,&s)
- #define ye cout<<“YES”<<endl
- #define mp(a,b) make_pair(a,b)
- #define mm memset
- #define ss second
- #define ff first
- #define no cout<<“NO”<<endl
- #define valid(x,y,row,col) (x>=1&&x<=row&&y>=1&&y<=col)
- #define pii pair<ll,ll>
- ll fx[]= {0,0,1,-1};
- ll fy[]= {1,-1,0,0};
- ll gcdd(ll a, ll b)
- {
- if(a==0)
- {
- return b;
- }
- while(b!=0)
- {
- if(a>b)
- {
- a-=b;
- }
- else
- {
- b-=a;
- }
- }
- return a;
- }
- int main()
- {
- ll n,m,t,j,i,l,z=0;
- bool ans=false;
- string s=””,cp;
- char ch;
- sc1(n);
- f(i,0,n)
- {
- cin>>ch;
- s+=ch;
- }
- map<string, ll>mp;
- mp[s]=0;
- queue<string>q;
- q.push(s);
- cp=s;
- sort(cp.begin(),cp.end());
- while(q.size()>0)
- {
- string st=q.front();
- q.pop();
- if(ans)
- br;
- f(i,2,n+1)
- {
- string ss=st;
- reverse(ss.begin(),ss.begin()+i);
- if(mp[ss]==0)
- {
- mp[ss]=mp[st]+1;
- cout<<“level of “<<ss<<“=”<<mp[ss]<<endl;
- if(ss==cp)
- {
- ans=true;
- br;
- }
- q.push(ss);
- }
- }
- }
- p(mp[cp]);
- return 0;
- }