# 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;****}**

__Sample input:__

__Output:__**level of 54231=1**

**level of 25431=1**

**level of 32541=1**

**level of 13254=1**

**level of 45231=2**

**level of 24531=2**

**level of 32451=2**

**level of 13245=2**

**level of 52431=2**

**level of 34521=2**

**level of 13452=2**

**level of 23541=2**

**level of 52341=2**

**level of 14523=2**

**level of 31254=2**

**level of 23154=2**

**level of 52314=2**

**level of 42531=3**

**level of 35421=3**

**level of 13542=3**

**level of 23451=3**

**level of 42351=3**

**level of 15423=3**

**level of 31245=3**

**level of 23145=3**

**level of 42315=3**

**level of 34251=3**

**level of 13425=3**

**level of 43521=3**

**level of 54321=3**

**level of 12543=3**

**level of 31452=3**

**level of 43152=3**

**level of 54312=3**

**level of 53241=3**

**level of 45321=3**

**level of 14532=3**

**level of 25341=3**

**level of 43251=3**

**level of 14325=3**

**level of 41523=3**

**level of 54123=3**

**level of 25413=3**

**level of 21354=3**

**level of 52134=3**

**level of 45213=3**

**level of 32154=3**

**level of 51324=3**

**level of 45132=3**

**level of 25314=3**

**level of 32514=3**

**level of 41325=3**

**level of 35241=4**

**level of 13524=4**

**level of 53421=4**

**level of 12453=4**

**level of 31542=4**

**level of 53142=4**

**level of 45312=4**

**level of 15432=4**

**level of 24351=4**

**level of 15324=4**

**level of 51423=4**

**level of 45123=4**

**level of 24513=4**

**level of 21345=4**

**level of 42135=4**

**level of 54213=4**

**level of 32145=4**

**level of 54132=4**

**level of 24315=4**

**level of 32415=4**

**level of 15243=4**

**level of 31425=4**

**level of 43125=4**

**level of 12534=4**

**level of 12345=4**

**check this UVA Online Judge problem**tutorial for Breadth First Search practice.

__To get my all-problem solution tutorial click here.__