Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
All possible strings of any length that can be formed from a given string?
In this section we will see how to generate all possible strings of any length from a given string. This approach takes each combination of characters to make strings and then generates all permutations of each combination. For example, if the string is "ABC", then it will generate − {A, B, C, AB, BA, BC, CB, CA, AC, ABC, ACB, BAC, BCA, CAB, CBA}
Syntax
void printAllString(char str[]);
Algorithm
printAllString(str)
Begin
n := length of the string str
count is 2^n - 1
for each number 1 to count, do
sub_str := empty string
for j in range 0 to n, do
if jth bit of the counter is set, then
concatenate jth character of str with sub_str
end if
done
repeat:
print sub_string
until next permutation of sub_string is not completed
done
End
Example
The following C program generates all possible strings by first creating subsets using bit manipulation and then generating permutations of each subset −
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
void swap(char *x, char *y) {
char temp = *x;
*x = *y;
*y = temp;
}
bool nextPermutation(char str[], int n) {
int i = n - 2;
while (i >= 0 && str[i] >= str[i + 1]) {
i--;
}
if (i < 0) return false;
int j = n - 1;
while (str[j] <= str[i]) {
j--;
}
swap(&str[i], &str[j]);
for (int k = i + 1, l = n - 1; k < l; k++, l--) {
swap(&str[k], &str[l]);
}
return true;
}
void sortString(char str[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (str[i] > str[j]) {
swap(&str[i], &str[j]);
}
}
}
}
void printAllString(char str[]) {
int n = strlen(str);
unsigned int count = (unsigned int)pow(2, n);
for (int counter = 1; counter < count; counter++) {
char subs[100] = "";
int subsLen = 0;
for (int j = 0; j < n; j++) {
if (counter & (1 << j)) {
subs[subsLen++] = str[j];
}
}
subs[subsLen] = '\0';
sortString(subs, subsLen);
do {
printf("%s<br>", subs);
} while (nextPermutation(subs, subsLen));
}
}
int main() {
char str[] = "ABC";
printf("All possible strings from '%s':<br>", str);
printAllString(str);
return 0;
}
Output
All possible strings from 'ABC': A B AB BA C AC CA BC CB ABC ACB BAC BCA CAB CBA
How It Works
- The algorithm uses bit manipulation to generate all possible subsets (2^n - 1 non-empty subsets)
- For each subset, it generates all permutations using the next permutation algorithm
- The bit counter determines which characters to include in each subset
- Each subset is sorted first to ensure permutations are generated in lexicographic order
Conclusion
This approach efficiently generates all possible strings by combining subset generation with permutation algorithms. The time complexity is O(n × 2^n × n!) in the worst case, where n is the length of the input string.
