Generating All Possible Strings From an Alphabet
Here you can download all possible strings of various lengths generated from the lowercase English alphabet as .txt files, and learn how to generate your own strings using Bash or PHP.

By. Jacob
Edited: 2026-08-10 07:42
Description: Given a set of N distinct characters and a desired string length L, generate every possible unique string of length L using those characters, with repetition allowed within each string.
Sometimes it may be useful to know the number of possible strings of a given length that can be formed from an alphabet. We can calculate this from the size of the alphabet. Since the English alphabet has 26 letters, there are 26 possible characters for each position in the string. For a 2-character string, this gives 26*26 = 26^2 = 676 possible strings.
This exercise will later allow you to generate all possible strings from basically any set of characters, including numbers, lowercase and uppercase letters, and even Unicode characters such as emoji.
It may be best to keep a database or file with pre-generated strings, or even better, just generate a given string when you need it. Since the strings are always generated in the same order, you can also keep a numeric record of which strings have already been used by a system and thereby ensure uniqueness.
Here is a table showing the number of possible strings that can be generated from the English alphabet for various string lengths:
| Length | Formula | Possible Strings | File? | Size |
|---|---|---|---|---|
| 2 | 26*26 or 26^2 | 676 | lowercase-alphabet-2-character-strings.txt | 2.1 KB |
| 3 | 26*26*26 or 26^3 | 17.576 | lowercase-alphabet-3-character-strings.txt | 70.4 KB |
| 4 | 26*26*26*26 or 26^4 | 456.976 | lowercase-alphabet-4-character-strings.txt | 2.3 MB |
| 5 | 26*26*26*26*26 or 26^5 | 11.881.376 | n/a | 71.3 MB |
| 6 | 26*26*26*26*26*26 or 26^6 | 308.915.776 | n/a | 2.1 GB |
Note. Each string in the files above is followed by a single line break (\n).
We do not necessarily need to create an algorithm to generate all possible strings. In a Bash shell, we can use brace expansion to generate characters from a given range using this simple syntax: {a..z}.
To output all possible two-letter strings using the 26 letters of the English alphabet:
echo {a..z}{a..z}
This will result in a space-separated list:
aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg...
Generate all possible strings with PHP
Being a web developer, I personally use PHP a lot, so I came up with a function that generates all possible strings from the items in an input array and saves them to a .txt file in batches.
The function is probably not 100% optimized, but it is fairly fast and does not use much memory, since it writes the generated strings to a file as it goes. You could probably optimize it further by determining the ideal buffer size for disk writes.
Here we go:
function generate_strings(
$max,
$fp = null,
&$c2 = null,
$i = 0,
&$pArr = [],
&$strings_buffer = [],
&$bytes_written = 0,
&$item_list = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
]
) {
$data = '';
// Remember the character from the previous loop
if (isset($c2)) {
$pArr["$i"] = $c2;
}
// Increment the counter +1 towards $max
++$i;
// Loop through the alphabet
foreach ($item_list as $c) {
// If we have reached max nesting, add what we got to the $strings_buffer array
if ($i === $max) {
// Combine characters passed on by previous iteration until we have been through all possible strings.
// The method will execute the following order, the "X" marks the current item.
// 1. aaaX
// 2. aaXz
// 3. aXzz
// 4. Xzzz
$string = implode('', $pArr);
$strings_buffer[] = $string . $c;
// Save in batches to make the function faster
$buffer_count = count($strings_buffer);
if ($buffer_count >= 80) {
// Add a single line break before imploding
// to avoid first item appearing on the same line as the previous when saving a batch
if ($bytes_written > 0) {
$data = "\n";
}
$data .= implode("\n", $strings_buffer);
// On fflush: If any buffering is used on the system, make sure we write data now
// fflush should typically be called right after fwrite
if (!fwrite($fp, $data) || !fflush($fp)) {
throw new Exception("Error writing to strings file.");
}
$bytes_written = $bytes_written + strlen($data);
$data = '';
$strings_buffer = [];
}
} else {
generate_strings($max, $fp, $c, $i, $pArr, $strings_buffer, $bytes_written, $item_list);
}
}
// Write potential leftover strings that did not reach the batch_limit
if (count($strings_buffer) > 0) {
if ($bytes_written > 0) {
$data = "\n";
}
$data .= implode("\n", $strings_buffer);
if (!fwrite($fp, $data) || !fflush($fp)) {
throw new Exception("Error writing to strings file.");
}
$bytes_written = $bytes_written + strlen($data);
$data = '';
$strings_buffer = [];
}
return $bytes_written;
}
You can call the function like this:
$file_path = 'strings-output-file.txt';
$fp = fopen($file_path, "a");
generate_strings(4, $fp);
Storing generated strings takes a lot of space
A bit of a warning, though: storing generated strings is probably unnecessary, at least when using them for URL shortening purposes. Instead, you could have a database table containing the URLs, using a BIGINT with AUTO_INCREMENT as the primary key, and generate a corresponding string from each numeric ID when needed.
This way, you could quickly insert new URLs while avoiding issues with concurrently stored URLs. Since the strings are generated deterministically in the same order, a specific numeric ID can always be mapped to the same string.
If you use my function as a starting point, you would therefore need to modify it so that it generates the string corresponding to a given numeric ID rather than generating and writing every possible string to a file.
This is possible because the generated strings always occur in the same order. For example, depending on the indexing convention used, 1 could correspond to aaaa, 2 to aaab, and so on.
Using a Bash script
You could also create a Bash script and place it in /usr/local/bin/generate_strings_2x.sh:
#!/bin/bash
echo {a..z}{a..z}
This can then be called from PHP with shell_exec:
echo shell_exec('generate_strings_2x.sh');
Since I use PHP a lot, I find shell_exec to be useful for interacting with other programs at times. However, this does probably work best if you are on a linux system.
Why are generated strings interesting?
I have no idea. But I personally had a need to generate all possible two-letter strings consisting of the 26 letters of the English alphabet. I was planning to use them for various purposes, such as creating a URL shortener or shortening long IDs in HTML.
A two-letter string gives me 676 possible strings, which should be more than enough to satisfy my needs. I also wanted to experiment with replacing longer text strings with shorter identifiers in order to save bandwidth for mobile users. As I have since tested for myself, however, GZIP and Brotli are generally better suited for reducing the size of textual data. Still, this has been a nice little programming challenge to solve.
The alphabet is also an ideal candidate to practice on. Once you have written an algorithm that works with the alphabet, you should also be able to adapt it to other sets of characters, including digits, uppercase and lowercase letters, symbols, and other Unicode characters such as emoji.
Permutations and combinations are different
In combinatorics, permutations and combinations have specific meanings. A permutation is an arrangement in which order matters, whereas a combination is a selection in which order does not matter. For example, the three distinct letters ABC can be arranged in 6 different ways, giving the following permutations without repetition:
abc acb bca bac cab cba
If repetition is allowed, we are no longer simply generating permutations of the three distinct letters. Instead, we can generate all possible strings of length 3 over the alphabet {a, b, c}. Since each of the three positions can contain any of the three letters, there are 33 = 27 possible strings:
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
Combinations
With combinations, the order of the selected items does not matter. For example, if we select three distinct letters from a larger set, ABC and CBA represent the same combination, whereas ABD and BDE represent different combinations:
- ABC
- CBA
- ABD
- BDE
Note. The list contains three unique combinations because ABC and CBA contain the same three elements. Their order does not matter when considering combinations, so CBA does not represent an additional combination.
Of course, this is more a matter of mathematics than PHP, but the distinction is nevertheless useful when working with algorithms that generate sets of values.
Terminology note. An earlier version of this article used the terms permutation and combination somewhat informally when referring to all possible strings generated from a given set of characters. The article has since been corrected to use the more precise mathematical terminology. In particular, sequences such as aa, ab, ba, and bb are described as possible strings rather than permutations, since repetition is allowed. The underlying algorithms and calculations remain the same; the correction primarily concerns terminology and how the concepts are explained.

Tell us what you think: