Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added backtracking algorithm named all permutations of a string/array #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
51 changes: 51 additions & 0 deletions cpp/include/algorithm/backtracking/all_permutations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Printing all the permutations of a string/array
--------------------
A permutation, also called an “arrangement number” or “order,” is a rearrangement
of the elements of an ordered list S into a one-to-one correspondence with S itself.
A string of length n has n! permutation. Generate all the permutations of the string.

Time complexity
---------------
O(N*N!), where N is the length of the string.

Space complexity
----------------
O(N!), where N is the length of the string.

Author
------
Brijesh Kumar (@brijeshsos66)
*/

#ifndef ALL_PERMUTATIONS_HPP
#define ALL_PERMUTATIONS_HPP

#include <algorithm>

/*
Below is the program for printing the permutations of a string. Inorder to perform the same on a array/vector
then convert the array elements into string(using to_string() function) and perform the same.
Here start is the starting point of a string intially equals to 0 and end is initially equals to length of the
string minus 1 (N - 1) as indices are starting from 0.
*/
void permutations(string str, int start, int end){
//BASE case
if(start == end)
cout<<str<<endl;
else{
for(int i=start;i<=end;i++){
//SWAP
swap(a[start],a[i]);

//RECURSION
permutations(str,start+1,end);

//BACKTRACKING
swap(a[start],a[i]);
}
}
}


#endif // ALL_PERMUTATIONS_HPP
29 changes: 29 additions & 0 deletions cpp/test/algorithm/backtracking/all_permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "third_party/catch.hpp"
#include "algorithm/backtracking/all_permutations.hpp"

TEST_CASE("Base cases","[backtracking][all_permutations]"){
string s =""; // empty string
vector<string> permutations = {""};
REQUIRE(all_permutations(s,0,s.length()-1) == permutations);

s = "a";
permutations = {"a"};
REQUIRE(all_permutations(s,0,s.length()-1) == permutations);

s = " ";
permutations = {" "};
REQUIRE(all_permutations(s,0,s.length()-1) == permutations);
}

TEST_CASE("Normal cases",[backtracking][all_permutations]"){
string s = "abc";
vector<string> permutations = {"abc","acb","bac","bca","cab","cba"};
REQUIRE(all_permutations(s,0,s.length()-1) == permutations);

s = "abcd";
permutations ={"abcd","abdc","acbd","acdb","adcb","adbc",
"bacd","badc","bcad","bcda","bdca","bdac",
"cbad","cbda","cabd","cadb","cdab","cdba",
"dbca","dbac","dcba","dcab","dacb","dabc" };
REQUIRE(all_permutations(s,0,s.length()-1) == permutations);
}