Skip to content
This repository was archived by the owner on Oct 16, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions string_permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
To find all possible permutation of letters in a given string
fro e.g. abc
abc
acb
bac
bca
cab
cba
TIME COMPLEXITY : O (n! * n)
*/

#include <iostream>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

void perm(string s, int visited[], string &l, int &count)
{
if (s.size() == l.size()) //Base case if generated string has same length as original string print it
{
count++; //add to count (total possible strings)
cout << l << endl;
return;
}
else
{
for (int i = 0; i < s.size(); i++)
{
if (visited[i] == 0) //if the character of string is not visited
{
visited[i] = 1; //mark it as visited
l = l + s[i]; //append it to the generated string
perm(s, visited, l, count); //call perm function again
l.pop_back(); //pop the last element
visited[i] = 0; //mark as unvisited
}
}
}
}

int main()
{
string s, l = "";
cout << "\nEnter String :"; //take string from user
cin >> s;
int visited[s.size()] = {0}; //set all values of visited to 0
int count = 0; //total possible strings
perm(s, visited, l, count);
cout << "\nTotal possible :" << count << endl;
return 0;
}