Skip to main content

Command Palette

Search for a command to run...

Permutations of a string: Exploring String Permutations

Published
1 min read
A

BTech CSE student at IIIT Ranchi | Aspiring Software Engineer | Passionate about coding, building innovative projects, and solving real-world problems through technology. Currently diving into web development, Machine learning and deep learning. Always learning, creating, and pushing boundaries! #Tech #Coding

Find all permutation of a string with unique characters..

//find all permutation of a string with unique characters..
#include<bits/stdc++.h>
using namespace std;

void permutation(string ans,string original){
    if(original.size()==0){
        cout<<ans<<endl;
        return;
    }
    for(int i=0;i<original.size();i++){
        char ch = original[i];
        string left = original.substr(0,i);
        string right = original.substr(i+1);
        permutation(ans+ch,left+right);
    }
}

int main()
{
    string str;
    cout<<"Enter the string : ";
    getline(cin,str);

    permutation("",str);

    return 0;
}

This method is not good in terms of time and space complexity but it is good to learn coding..

##we can improve it by the concept of “Backtracking”..

In conclusion, exploring the permutations of a string with unique characters provides valuable insights into the fundamental concepts of coding and algorithm design. While the basic method may not be optimal in terms of time and space complexity, it serves as an excellent learning tool. By employing backtracking techniques, we can significantly enhance the efficiency of generating permutations, making it a worthwhile endeavor for those looking to deepen their understanding of algorithmic problem-solving.

More from this blog

Untitled Publication

28 posts

Currently pursuing Bachelor of technology (Computer science and Engineering) from INDIAN INSTITUTE OF INFORMATION TECHNOLOGY RANCHI,JHARKHAND..