All Projects → bpolaszek → string-combinations

bpolaszek / string-combinations

Licence: MIT license
A simple, low-memory footprint function to generate all string combinations from a series of characters.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to string-combinations

Superstring
A fast and memory-optimized string library for C++
Stars: ✭ 252 (+908%)
Mutual labels:  string, memory
compact str
A memory efficient string type that can store up to 24* bytes on the stack
Stars: ✭ 322 (+1188%)
Mutual labels:  string, memory
stringy
Convert string to camel case, snake case, kebab case / slugify, custom delimiter, pad string, tease string and many other functionalities with help of by Stringy package.
Stars: ✭ 137 (+448%)
Mutual labels:  string, manipulation
tsdom
Fast, lightweight TypeScript DOM manipulation utility
Stars: ✭ 16 (-36%)
Mutual labels:  lightweight, manipulation
memory signature
A small wrapper class providing an unified interface to search for various memory signatures
Stars: ✭ 69 (+176%)
Mutual labels:  lightweight, memory
Viperhtml
Isomorphic hyperHTML
Stars: ✭ 318 (+1172%)
Mutual labels:  lightweight, manipulation
Hyperhtml
A Fast & Light Virtual DOM Alternative
Stars: ✭ 2,872 (+11388%)
Mutual labels:  lightweight, manipulation
Lwmem
Lightweight dynamic memory manager library for embedded systems with memory constraints. It implements malloc, calloc, realloc and free functions
Stars: ✭ 92 (+268%)
Mutual labels:  lightweight, memory
Herbe
Daemon-less notifications without D-Bus. Minimal and lightweight.
Stars: ✭ 235 (+840%)
Mutual labels:  lightweight
Yay Evil Emacs
😈 A lightweight literate Emacs config with even better "better defaults". Shipped with a custom theme!
Stars: ✭ 250 (+900%)
Mutual labels:  lightweight
Imdn
Lightweight Image Super-Resolution with Information Multi-distillation Network (ACM MM 2019, Winner Award of ICCVW AIM 2019 Constrained SR Track1&Track2)
Stars: ✭ 229 (+816%)
Mutual labels:  lightweight
Libuwsc
A Lightweight and fully asynchronous WebSocket client library based on libev
Stars: ✭ 237 (+848%)
Mutual labels:  lightweight
Api
Minimal, extremely fast, lightweight Ruby framework for HTTP APIs
Stars: ✭ 252 (+908%)
Mutual labels:  lightweight
V Emoji Picker
🌟 A Lightweight and customizable package of Emoji Picker in Vue using emojis natives (unicode).
Stars: ✭ 231 (+824%)
Mutual labels:  lightweight
simple-jwt-provider
No description or website provided.
Stars: ✭ 33 (+32%)
Mutual labels:  lightweight
Mu
A tweet-sized PHP micro-router
Stars: ✭ 229 (+816%)
Mutual labels:  lightweight
Snackbar
A tiny browser library for showing a brief message at the bottom of the screen (1kB gzipped).
Stars: ✭ 224 (+796%)
Mutual labels:  lightweight
hugoblog
Hugoblog is responsive, simple, and clean that would fit for your personal blog based on Hugo Theme Static Site Generator (SSG)
Stars: ✭ 48 (+92%)
Mutual labels:  lightweight
Moveto
A lightweight scroll animation javascript library without any dependency
Stars: ✭ 2,746 (+10884%)
Mutual labels:  lightweight
Gunslinger
C99, header-only framework for games and multimedia applications
Stars: ✭ 246 (+884%)
Mutual labels:  lightweight

Latest Stable Version License CI Workflow Coverage Quality Score Total Downloads

String combinations

A simple, low-memory footprint function to generate all string combinations from a series of characters.

Installation

PHP 7.4+ is required.

composer require bentools/string-combinations

Usage

I want to get all combinations with the letters a, b, c.

require_once __DIR__ . '/vendor/autoload.php';
use function BenTools\StringCombinations\string_combinations;

foreach (string_combinations('abc') as $combination) { // Can also be string_combinations(['a', 'b', 'c'])
    echo $combination . PHP_EOL;
}

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

Array output

It will dump all combinations into an array.

var_dump(string_combinations('abc')->asArray());

Count combinations

It will return the number of possible combinations.

var_dump(count(string_combinations('abc'))); // 39

Specifying min length, max length

foreach (string_combinations('abc', $min = 2, $max = 2) as $combination) {
    echo $combination . PHP_EOL;
}

Output:

aa
ab
ac
ba
bb
bc
ca
cb
cc

Using an array as first argument, and a separator

foreach (string_combinations(['woof', 'meow', 'roar'], 2, 3, '-') as $combination) {
    echo $combination . PHP_EOL;
}

Output:

woof-woof
woof-meow
woof-roar
meow-woof
meow-meow
meow-roar
roar-woof
roar-meow
roar-roar
woof-woof-woof
woof-woof-meow
woof-woof-roar
woof-meow-woof
woof-meow-meow
woof-meow-roar
woof-roar-woof
woof-roar-meow
woof-roar-roar
meow-woof-woof
meow-woof-meow
meow-woof-roar
meow-meow-woof
meow-meow-meow
meow-meow-roar
meow-roar-woof
meow-roar-meow
meow-roar-roar
roar-woof-woof
roar-woof-meow
roar-woof-roar
roar-meow-woof
roar-meow-meow
roar-meow-roar
roar-roar-woof
roar-roar-meow
roar-roar-roar

No duplicates

You can avoid generating stings having the same letter more than once with the withoutDuplicates() method:

$combinations = string_combinations('abc');
var_dump(count($combinations)); // 39
print_r($combinations->asArray());

Array ( [0] => a [1] => b [2] => c [3] => aa [4] => ab [5] => ac [6] => ba [7] => bb [8] => bc [9] => ca [10] => cb [11] => cc [12] => aaa [13] => aab [14] => aac [15] => aba [16] => abb [17] => abc [18] => aca [19] => acb [20] => acc [21] => baa [22] => bab [23] => bac [24] => bba [25] => bbb [26] => bbc [27] => bca [28] => bcb [29] => bcc [30] => caa [31] => cab [32] => cac [33] => cba [34] => cbb [35] => cbc [36] => cca [37] => ccb [38] => ccc )

$combinations = $combinations->withoutDuplicates();
var_dump(count($combinations)); // 15
print_r($combinations->asArray());

Array ( [0] => a [1] => b [2] => c [3] => ab [4] => ac [5] => ba [6] => bc [7] => ca [8] => cb [9] => abc [10] => acb [11] => bac [12] => bca [13] => cab [14] => cba )

Performance considerations

Because it uses Generators to generate all possible combinations, you can iterate over thousands of possibilities without losing a single MB.

This has been executed on my Core i7 personnal computer with 8GB RAM:

require_once __DIR__ . '/vendor/autoload.php';
use function BenTools\StringCombinations\string_combinations;

$start = microtime(true);
$combinations = string_combinations('abcd1234');
foreach ($combinations as $c => $combination) {
    continue;
}
$end = microtime(true);

printf(
    'Generated %d combinations in %ss - Memory usage: %sMB / Peak usage: %sMB' . PHP_EOL,
    ++$c,
    round($end - $start, 3),
    round(memory_get_usage(true) / 1024 / 1024),
    round(memory_get_peak_usage(true) / 1024 / 1024)
);

Output:

Generated 19173960 combinations in 5.579s - Memory usage: 2MB / Peak usage: 2MB

Tests

./vendor/bin/phpunit

See also

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].