diff --git a/README.md b/README.md index 41b06950..1a9faa6d 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ handle it from there. :smile: * Backtracking * [N-Queens](cpp/include/algorithm/backtracking/n_queens.hpp) :white_check_mark: + * [All permutations of a string/array](cpp/include/algorithm/backtracking/all_permutations.hpp) :white_check_mark: * Dynamic programming * [0-1 knapsack](cpp/include/algorithm/dynamic_programming/0_1_knapsack.hpp) :white_check_mark: diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 9d9a16ee..fdfd387a 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -28,6 +28,11 @@ add_executable(n_queens test/algorithm/backtracking/n_queens.cpp) target_link_libraries(n_queens test_runner) +# ALL permutations of a string/array +add_executable(all_permutations + test/algorithm/backtracking/all_permutations.cpp) +target_link_libraries(all_permutations test_runner) + # ------------------- # Dynamic programming # ------------------- diff --git a/cpp/include/algorithm/backtracking/all_permutations.hpp b/cpp/include/algorithm/backtracking/all_permutations.hpp new file mode 100644 index 00000000..b4b3e96e --- /dev/null +++ b/cpp/include/algorithm/backtracking/all_permutations.hpp @@ -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 + +/* +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< 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 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); +} \ No newline at end of file