Sort a string lexicographically using triple cyclic shifts

• Difficulty Level : Medium
• Last Updated : 25 Sep, 2020

Given a string consisting of the first N distinct alphabets, the task is to sort the string by using at most N/2 moves. Each move involves the following:

• Select any 3 distinct indices.
• Perform a cyclic shift on the alphabets at these indices.

If it is possible to sort the strings, print the count of required moves. Otherwise, print “Not Possible”.

Examples:

Input: str = “cbda”
Output:
possible

Explanation:
Selecting the indices 0, 2 and 3 and performing a single circular shift among them, the given string “cbda” is converted to “abcd”.

Input: str = “cba”
Output: Not Possible

Approach:
In order to solve the problem, follow the steps below:

• Store the integers denoting correct of characters of the string in a vector .
• Place those elements correctly which can all occupy correct indices in a single cycle.
• Traverse an element of the vector
• If the element is not at its sorted index position, check if two or more numbers can be placed at correct indices in one cycle. If the condition is satisfied, perform the cycle otherwise check if there is a distinct index that does not hold the correct element available. If the condition is satisfied, select this index as the third index of the cycle and perform the cycle. If neither of the above conditions are satisfied, sorting would be impossible. Hence, break out of the loop and print “Not Possible”.
• Once a cyclic shift is performed, store the indices involved in the shift.
• If the element is at its sorted position, move to the next index.
• Repeat the above two steps for all vector elements.
• After the traversal is completed, if the entire array is in sorted order, print the shifts required. Else print “Not Possible”.

Below is the implementation of the above approach:

C++

 // C++ Program for sorting a// string using cyclic shift// of three indices#include using namespace std; void sortString(vector& arr, int n,                int moves){     // Store the indices    // which haven't attained    // its correct position    vector pos;    // Store the indices    // undergoing cyclic shifts    vector > indices;     bool flag = false;     for (int i = 0; i < n; i++) {         // If the element is not at        // it's correct position        if (arr[i] != i) {             // Check if all 3 indices can be            // placed to respective correct            // indices in a single move            if (arr[arr[arr[i]]] == i                && arr[arr[i]] != i) {                 int temp = arr[arr[i]];                indices.push_back({ i, arr[i],                                    arr[arr[i]] });                swap(arr[i], arr[arr[i]]);                swap(arr[i], arr[temp]);            }        }         // If the i-th index is still        // not present in its correct        // position, store the index        if (arr[i] != i) {            pos.push_back(i);        }    }     for (int i = 0; i < n; i++) {         if (arr[i] != i) {             int pos1 = i, pos2 = arr[i];            int pos3 = arr[arr[i]];             // To check if swapping two indices            // places them in their correct            // position            if (pos3 != pos1) {                 indices.push_back({ pos1,                                    pos2,                                    pos3 });                swap(arr[pos1], arr[pos2]);                swap(arr[pos1], arr[pos3]);                pos.erase(find(                    pos.begin(),                    pos.end(), pos2));                pos.erase(find(                    pos.begin(),                    pos.end(), pos3));                 if (arr[pos1] == pos1) {                    pos.erase(find(                        pos.begin(),                        pos.end(),                        pos1));                }            }            else {                 if (pos3                    == *pos.begin()) {                     if (*pos.begin()                        != pos.back()) {                        auto it                            = ++pos.begin();                        pos3 = *(it);                         if (*it != pos.back()                            && pos3 == pos2) {                            pos3 = *(++it);                        }                        else if (*it == pos.back()                                 && pos3 == pos2) {                             flag = true;                            break;                        }                    }                    else {                        flag = true;                        break;                    }                }                 indices.push_back({ pos1, pos2,                                    pos3 });                swap(arr[pos1], arr[pos2]);                swap(arr[pos1], arr[pos3]);                pos.erase(find(                    pos.begin(),                    pos.end(),                    pos2));            }        }         if (arr[i] != i) {            i--;        }    }     if (flag == true        || indices.size() > moves) {         cout << "Not Possible" << endl;    }    else {        cout << indices.size() << endl;         // Inorder to see the indices that        // were swapped in rotations,        // uncomment the below code         /*        for (int i = 0; i < indices.size();             i++) {             cout << indices[i] << " "                 << indices[i] << " "                 << indices[i] << endl;        }       */    }} // Driver Codeint main(){     string s = "adceb";    vector arr;     for (int i = 0; i < s.size(); i++) {        arr.push_back(s[i] - 'a');    }     sortString(arr, s.size(),               floor(s.size() / 2));}

Python3

 # Python3 program for sorting a# string using cyclic shift# of three indicesimport math def sortString(arr, n, moves):         # Store the indices    # which haven't attained    # its correct position    pos = []         # Store the indices    # undergoing cyclic shifts    indices = []    flag = False         for i in range(n):                 # If the element is not at        # it's correct position        if (arr[i] != i):                         # Check if all 3 indices can be            # placed to respective correct            # indices in a single move            if (arr[arr[arr[i]]] == i and                arr[arr[i]] != i):                temp = arr[arr[i]]                                 indices.append([i, arr[i],                               arr[arr[i]]])                 sw = arr[i]                arr[i] = arr[arr[i]]                arr[sw] = sw                 sw = arr[i]                arr[i] = arr[temp]                arr[temp] = sw         # If the i-th index is still        # not present in its correct        # position, store the index        if (arr[i] != i):            pos.append(i)                 for i in range(n):        if (arr[i] != i):            pos1 = i            pos2 = arr[i]            pos3 = arr[arr[i]]                         # To check if swapping two indices            # places them in their correct            # position            if (pos3 != pos1):                indices.append([pos1, pos2, pos3])                arr[pos1], arr[pos2] = arr[pos2], arr[pos1]                arr[pos1], arr[pos3] = arr[pos3], arr[pos1]                                 pos.remove(pos2)                                 if pos3 in pos:                    pos.remove(pos3)                                     if (arr[pos1] == pos1):                    pos.remove(pos1)                                 else:                if (pos3 == pos):                    it = 0                                         if (pos != pos[-1]):                        it = it + 1                        pos3 = pos[it]                                                 if (pos[it] != pos[-1] and                               pos3 == pos2):                            it = it + 1                            pos3 = pos[it]                                                     elif (pos[it] == pos[-1] and                                 pos3 == pos2):                            flag = True                            break                                             else:                        flag = True                        break                                     indices.append([pos1, pos2, pos3])                arr[pos1], arr[pos2] = arr[pos2], arr[pos1]                arr[pos1], arr[pos3] = arr[pos3], arr[pos1]                pos.remove(pos2)                         if (arr[i] != i):            i = i - 1                 if (flag == True or len(indices) > moves):        print("Not Possible")    else:                 # Inorder to see the indices that        # were swapped in rotations,        # uncomment the below code         # for i in range len(indices):        #    print (indices[i],        #           indices[i], indices[i])        print(len(indices)) # Driver codes = "adceb"arr = [] for i in s:    arr.append(ord(i) - ord('a'))     sortString(arr, len(s), math.floor(len(s) / 2)) # This code is contributed by costheta_z
Output:
1

My Personal Notes arrow_drop_up