Generate all passwords from given character set

Given a set of characters generate all possible passwords from them. This means we should generate all possible permutations of words using the given characters, with repititions and also upto a given length.

Examples:

Input : arr[] = {a, b}, 
          len = 2.
Output :
a b aa ab ba bb

The solution is to use recursion on the given character array. The idea is to pass all possible lengths and an empty string initially to a helper function. In the helper function we keep appending all the characters one by one to the current string and recur to fill the remaining string till the desired length is reached.

It can be better visualized using the below recursion tree:

     (a, b)
     /   \
        a     b
       / \   / \
      aa  ab ba bb

Following is the C++ implementation of the above method.

// C++ program to generate all passwords for given characters
#include <bits/stdc++.h>
using namespace std;
// int cnt;
// Recursive helper function, adds/removes characters
// until len is reached
void generate(char* arr, int i, string s, int len)
{
    // base case
    if (i == 0) // when len has been reached
    {
        // print it out
        cout << s << "\n";
        // cnt++;
        return;
    }
    // iterate through the array
    for (int j = 0; j < len; j++) {
        // Create new string with next character
        // Call generate again until string has
        // reached its len
        string appended = s + arr[j];
        generate(arr, i - 1, appended, len);
    }
    return;
}
// function to generate all possible passwords
void crack(char* arr, int len)
{
    // call for all required lengths
    for (int i = 1; i <= len; i++) {
        generate(arr, i, "", len);
    }
}
// driver function
int main()
{
    char arr[] = { 'a', 'b', 'c' };
    int len = sizeof(arr) / sizeof(arr[0]);
    crack(arr, len);
    cout << cnt << endl;
    return 0;
}

Output:

a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc

If we want to see the count of the words, we can uncomment the lines having cnt variable in the code. We can observe that it comes out to be  n^1 + n^2 +..+ n^n , where n = len. Thus the time complexity of the program is also O(n * n^n), hence exponential. We can also check for a particular password
while generating and break the loop and return if it is found. We can also include other symbols to be generated and if needed remove duplicates by preprocessing the input using a HashTable.

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar