Generate all passwords from given character set
Last Updated :
25 Apr, 2023
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 repetitions 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 implementation of the above method.
C++
#include <bits/stdc++.h>
using namespace std;
void generate( char * arr, int i, string s, int len)
{
if (i == 0)
{
cout << s << "\n" ;
return ;
}
for ( int j = 0; j < len; j++) {
string appended = s + arr[j];
generate(arr, i - 1, appended, len);
}
return ;
}
void crack( char * arr, int len)
{
for ( int i = 1; i <= len; i++) {
generate(arr, i, "" , len);
}
}
int main()
{
char arr[] = { 'a' , 'b' , 'c' };
int len = sizeof (arr) / sizeof (arr[0]);
crack(arr, len);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void generate( char [] arr, int i, String s, int len)
{
if (i == 0 )
{
System.out.println(s);
return ;
}
for ( int j = 0 ; j < len; j++)
{
String appended = s + arr[j];
generate(arr, i - 1 , appended, len);
}
return ;
}
static void crack( char [] arr, int len)
{
for ( int i = 1 ; i <= len; i++)
{
generate(arr, i, "" , len);
}
}
public static void main(String[] args)
{
char arr[] = { 'a' , 'b' , 'c' };
int len = arr.length;
crack(arr, len);
}
}
|
Python 3
def generate(arr, i, s, len ):
if (i = = 0 ):
print (s)
return
for j in range ( 0 , len ):
appended = s + arr[j]
generate(arr, i - 1 , appended, len )
return
def crack(arr, len ):
for i in range ( 1 , len + 1 ):
generate(arr, i, "", len )
arr = [ 'a' , 'b' , 'c' ]
len = len (arr)
crack(arr, len )
|
C#
using System;
class GFG
{
static void generate( char [] arr, int i, String s, int len)
{
if (i == 0)
{
Console.WriteLine(s);
return ;
}
for ( int j = 0; j < len; j++)
{
String appended = s + arr[j];
generate(arr, i - 1, appended, len);
}
return ;
}
static void crack( char [] arr, int len)
{
for ( int i = 1; i <= len; i++)
{
generate(arr, i, "" , len);
}
}
public static void Main(String[] args)
{
char []arr = { 'a' , 'b' , 'c' };
int len = arr.Length;
crack(arr, len);
}
}
|
Javascript
<script>
function generate(arr, i, s, len)
{
if (i == 0)
{
document.write(s + "<br/>" );
return ;
}
for (let j = 0; j < len; j++)
{
let appended = s + arr[j];
generate(arr, i - 1, appended, len);
}
return ;
}
function crack(arr, len)
{
for (let i = 1; i <= len; i++)
{
generate(arr, i, "" , len);
}
}
let arr = [ 'a' , 'b' , 'c' ];
let len = arr.length;
crack(arr, len);
</script>
|
Outputa
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 , where n = len. Thus the time complexity of the program is also , 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.
Approach#2: Using itertools
The approach used in this code is to generate all possible passwords of length up to ‘length’ using the characters in the array ‘arr’. The itertools.product() method is used to generate all possible combinations of length up to ‘length’ of the characters in ‘arr’, and the extend() method is used to add these combinations to the ‘passwords’ list.
Algorithm
1. Initialize an empty list ‘passwords’.
2. For i in range(1, length+1):
a. Generate all possible combinations of length i of the characters in ‘arr’ using itertools.product() method.
b. Convert each combination to a string using the join() method.
c. Add these strings to the ‘passwords’ list using the extend() method.
3. Return the ‘passwords’ list.
Python3
import itertools
def generate_passwords(arr, length):
passwords = []
for i in range ( 1 , length + 1 ):
passwords.extend([''.join(x) for x in itertools.product(arr, repeat = i)])
return passwords
arr = [ 'a' , 'b' , 'c' ]
length = len (arr)
print (generate_passwords(arr, length))
|
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']
Time complexity: O(n^m), where n is the length of ‘arr’ and m is the maximum length of the password. This is because the itertools.product() method generates all possible combinations of length up to ‘length’ of the characters in ‘arr’, and there can be n^m such combinations.
Space complexity: O(n^m), because the ‘passwords’ list will contain n^m elements at most, and each element can have a length of up to m. Additionally, the itertools.product() method generates all possible combinations of length up to ‘length’ of the characters in ‘arr’, which can also take up space. However, the space used by itertools.product() is negligible compared to the space used by the ‘passwords’ list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...