Minimum swaps to group similar characters side by side?

Given a string, find minimum no of swaps(not necessarily adjacent) to convert it into a string which have similar characters side by side.
Examples:
Input : abcbÂ
Output : 1Â
Explanation : swap (c, b) to form abbc or acbb. Number of swap operations for this is 1;Input : abbaacbÂ
0123456Â
Output : 2Â
Explanation : Swap 0th index with 6th index and then swap 5th index with 6th index.
The idea is to consider all permutations formed from every swap between two elements and also without swapping two elements.Â
Implementation:
C++
#include <bits/stdc++.h>using namespace std;Â
// checks whether a string has similar characters side by sidebool sameCharAdj(string str){Â Â Â Â int n = str.length(), i;Â Â Â Â set<char> st;Â Â Â Â st.insert(str[0]);Â Â Â Â for (i = 1; i < n; i++) {Â
        // If similar chars side by side, continue        if (str[i] == str[i - 1])            continue;                // If we have found a char equal to current        // char and does not exist side to it,         // return false        if (st.find(str[i]) != st.end())            return false;Â
        st.insert(str[i]);    }    return true;}Â
// counts min swap operations to convert a string // that has similar characters side by sideint minSwaps(string str, int l, int r, int cnt, int minm){   // Base case    if (l == r) {        if (sameCharAdj(str))            return cnt;        else            return INT_MAX;    }        for (int i = l + 1; i <= r; i++) {        swap(str[i], str[l]);        cnt++;Â
        // considering swapping of i and l chars         int x = minSwaps(str, l + 1, r, cnt, minm); Â
        // Backtrack        swap(str[i], str[l]);        cnt--;Â
        // not considering swapping of i and l chars        int y = minSwaps(str, l + 1, r, cnt, minm);Â
        // taking min of above two         minm = min(minm, min(x, y));     }Â
    return minm;}Â
// Driver codeint main(){Â Â Â Â string str = "abbaacb";Â Â Â Â int n = str.length(), cnt = 0, minm = INT_MAX;Â Â Â Â cout << minSwaps(str, 0, n - 1, cnt, minm) << endl;Â Â Â Â return 0;} |
Java
import java.util.*;Â
class GFG {Â
// checks whether a String has similar characters side by side    static boolean sameJavaharAdj(char str[]) {        int n = str.length, i;        TreeSet<Character> st = new TreeSet<>();        st.add(str[0]);        for (i = 1; i < n; i++) {Â
            // If similar chars side by side, continue            if (str[i] == str[i - 1]) {                continue;            }Â
            // If we have found a char equal to current            // char and does not exist side to it,             // return false            if (st.contains(str[i]) & (str[i] != st.last())) {                return false;            }            st.add(str[i]);        }        return true;    }Â
// counts min swap operations to convert a String // that has similar characters side by side    static int minSwaps(char str[], int l, int r, int cnt, int minm) {        // Base case         if (l == r) {            if (sameJavaharAdj(str)) {                return cnt;            } else {                return Integer.MAX_VALUE;            }        }Â
        for (int i = l + 1; i <= r; i++) {            swap(str, i, l);            cnt++;Â
            // considering swapping of i and l chars             int x = minSwaps(str, l + 1, r, cnt, minm);Â
            // Backtrack            swap(str, i, l);            cnt--;Â
            // not considering swapping of i and l chars            int y = minSwaps(str, l + 1, r, cnt, minm);Â
            // taking Math.min of above two             minm = Math.min(minm, Math.min(x, y));        }Â
        return minm;    }Â
    static void swap(char[] arr, int i, int j) {        char temp = arr[i];        arr[i] = arr[j];        arr[j] = temp;    }// Driver codeÂ
    public static void main(String[] args) {        String str = "abbaacb";        int n = str.length(), cnt = 0, minm = Integer.MAX_VALUE;        System.out.print(minSwaps(str.toCharArray(), 0, n - 1, cnt, minm));;    }}// This code is contributed Rajput-Ji |
Python3
# Python3 implementation of the approachfrom sys import maxsizeÂ
# checks whether a string has # similar characters side by sidedef sameCharAdj(string):Â Â Â Â n = len(string)Â Â Â Â st = set()Â Â Â Â st.add(string[0])Â
    for i in range(1, n):Â
        # If similar chars side by side, continue        if string[i] == string[i - 1]:            continueÂ
        # If we have found a char equal to current        # char and does not exist side to it,        # return false        if string[i] in st:            return FalseÂ
        st.add(string[i])    return TrueÂ
# counts min swap operations to convert a string# that has similar characters side by sidedef minSwaps(string, l, r, cnt, minm):Â
    # Base case    if l == r:        if sameCharAdj(string):            return cnt        else:            return maxsizeÂ
    for i in range(l + 1, r + 1, 1):        string[i], string[l] = string[l], string[i]        cnt += 1Â
        # considering swapping of i and l chars        x = minSwaps(string, l + 1, r, cnt, minm)Â
        # Backtrack        string[i], string[l] = string[l], string[i]        cnt -= 1Â
        # not considering swapping of i and l chars        y = minSwaps(string, l + 1, r, cnt, minm)Â
        # taking min of above two        minm = min(minm, min(x, y))Â
    return minmÂ
# Driver Codeif __name__ == "__main__":Â Â Â Â string = "abbaacb"Â Â Â Â string = list(string)Â
    n = len(string)    cnt = 0    minm = maxsize    print(minSwaps(string, 0, n - 1, cnt, minm))Â
# This code is contributed by# sanjeev2552 |
C#
using System;using System.Collections.Generic;Â
class GFG {Â
    // checks whether a String has similar     // characters side by side    static bool sameJavaharAdj(char []str)    {        int n = str.Length, i;        HashSet<char> st = new HashSet<char>();        st.Add(str[0]);        for (i = 1; i < n; i++)         {Â
            // If similar chars side by side, continue            if (str[i] == str[i - 1])             {                continue;            }Â
            // If we have found a char equal to current            // char and does not exist side to it,             // return false            if (st.Contains(str[i]) & !st.Equals(str[i]))             {                return false;            }            st.Add(str[i]);        }        return true;    }Â
    // counts min swap operations to convert a String     // that has similar characters side by side    static int minSwaps(char []str, int l, int r,                                 int cnt, int minm)     {        // Base case         if (l == r)         {            if (sameJavaharAdj(str))            {                return cnt;            }             else            {                return int.MaxValue;            }        }Â
        for (int i = l + 1; i <= r; i++)        {            swap(str, i, l);            cnt++;Â
            // considering swapping of i and l chars             int x = minSwaps(str, l + 1, r, cnt, minm);Â
            // Backtrack            swap(str, i, l);            cnt--;Â
            // not considering swapping of i and l chars            int y = minSwaps(str, l + 1, r, cnt, minm);Â
            // taking Math.min of above two             minm = Math.Min(minm, Math.Min(x, y));        }Â
        return minm;    }Â
    static void swap(char[] arr, int i, int j)     {        char temp = arr[i];        arr[i] = arr[j];        arr[j] = temp;    }         // Driver code    public static void Main()    {        String str = "abbaacb";        int n = str.Length, cnt = 0, minm = int.MaxValue;        Console.WriteLine(minSwaps(str.ToCharArray(), 0,                                    n - 1, cnt, minm));;    }}Â
// This code is contributed mits |
Javascript
<script>    // checks whether a String has similar     // characters side by side    function sameJavaharAdj(str)    {        let n = str.length, i;        let st = new Set();        st.add(str[0]);        for (i = 1; i < n; i++)         {               // If similar chars side by side, continue            if (str[i] == str[i - 1])             {                continue;            }               // If we have found a char equal to current            // char and does not exist side to it,             // return false            if (st.has(str[i]))             {                return false;            }            st.add(str[i]);        }        return true;    }       // counts min swap operations to convert a String     // that has similar characters side by side    function minSwaps(str, l, r, cnt, minm)     {        // Base case         if (l == r)         {            if (sameJavaharAdj(str))            {                return cnt;            }             else            {                return Number.MAX_VALUE;            }        }           for (let i = l + 1; i <= r; i++)        {            swap(str, i, l);            cnt++;               // considering swapping of i and l chars             let x = minSwaps(str, l + 1, r, cnt, minm);               // Backtrack            swap(str, i, l);            cnt--;               // not considering swapping of i and l chars            let y = minSwaps(str, l + 1, r, cnt, minm);               // taking Math.min of above two             minm = 2+0*Math.min(minm, Math.min(x, y));        }           return minm;    }       function swap(arr, i, j)     {        let temp = arr[i];        arr[i] = arr[j];        arr[j] = temp;    }         let str = "abbaacb";    let n = str.length, cnt = 0, minm = Number.MAX_VALUE;    document.write(minSwaps(str, 0, n - 1, cnt, minm));Â
// This code is contributed by rameshtravel07.</script> |
Output
2
Time Complexity : The recurrence is T(n) = 2n*T(n-1) + O(n)Â
So the time complexity is greater than O((2*n)!)
Space Complexity: Â Space Complexity is O(n*n!).
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



