Finding all permutations that match a set of rules
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If you need all permutations that satisfy a set of rules, the core problem is not permutation generation alone. It is permutation generation plus pruning: the algorithm must reject partial arrangements as early as possible instead of creating every possible ordering and filtering them afterward.
Why Brute Force Gets Expensive Fast
For n elements, there are n! permutations. That explodes quickly:
- '
5! = 120' - '
8! = 40320' - '
10! = 3628800'
If you generate all of them and only then check the rules, most of the work is wasted. The better approach is backtracking with constraint checks on partial solutions.
Use Backtracking with Early Rejection
Backtracking builds the permutation one position at a time. After each placement, you test whether the partial arrangement already violates any rule. If it does, stop exploring that branch.
This is exactly what makes the method practical. A rule like "A cannot be next to B" can often be checked as soon as one of them is placed beside the other, instead of waiting until the entire permutation is complete.
Example Problem
Suppose we want permutations of ["A", "B", "C", "D"] with these rules:
- '
Amust appear beforeC' - '
Bcannot be adjacent toD' - '
Ccannot be in the first position'
A backtracking solver can encode these checks directly.
This still searches systematically, but it avoids exploring obviously invalid branches.
Different Rule Types Need Different Checks
Not all rules are equally easy to test. A useful classification is:
- position rules, such as "X must be in slot 3"
- adjacency rules, such as "Y cannot follow Z"
- ordering rules, such as "P must appear before Q"
- global rules, such as "the first three positions must contain exactly two vowels"
Position and adjacency rules are excellent for early pruning because they can often be checked immediately. Global rules may need more careful bounding logic to prune effectively before the permutation is complete.
Represent Constraints So They Are Cheap
A common mistake is to write constraint checks that repeatedly scan the whole partial permutation. That is acceptable for small inputs, but it becomes expensive when the search tree is large.
For bigger problems, maintain auxiliary state:
- a
usedarray for membership - current positions of important symbols
- counters for categories
- predecessor or dependency maps
That turns each pruning step into a small constant-time check instead of a repeated linear scan.
When This Becomes a Constraint-Satisfaction Problem
At some point, the problem is better viewed as a general constraint-satisfaction problem rather than a "permutations" problem. If the rules are complex, techniques such as forward checking, arc consistency, or a CSP solver may outperform handwritten brute-force backtracking.
Still, plain backtracking remains the right first tool for many interview, puzzle, and medium-sized search problems because it is simple and surprisingly effective when pruning is strong.
Common Pitfalls
- Generating every permutation first and filtering later wastes factorial work that pruning could avoid.
- Checking only full permutations misses the main advantage of backtracking, which is early rejection.
- Repeatedly scanning the whole partial arrangement for every rule makes the solver slower than necessary.
- Encoding ordering rules incorrectly can reject valid branches too early or too late.
- Forgetting that some rules interact means a branch may look valid under each rule separately but fail once the rules are combined.
Summary
- The practical way to find valid permutations is backtracking plus early rule checks.
- Strong pruning matters more than clever permutation-generation tricks.
- Position, adjacency, and ordering rules can often be checked on partial permutations.
- For larger or more complex rule sets, treat the problem as a constraint-satisfaction problem and carry more state to prune cheaply.
Related reading
- Finding all permutations to get the given sum Coin change problem
- Finding all possible combinations of numbers to reach a given sum
- Finding all possible combined plus and minus sums of n arguments?
- Finding all the common substrings of given two strings
- Finding all the subsets of a set
- Finding all the unique permutations of a string without generating duplicates
- Finding all the shortest paths between two nodes in unweighted undirected graph
- Finding an axis-aligned rectangle inside a polygon

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.