top of page

Excel Arena

This is the place, where we can test the boundaries of Excel functions.

Excel Formula Magic: Calculate the Number of Unique Permutations of a Word Using a Single Formula!

10m
4 min read

Have you ever wondered how many different words or character arrangements can be created from a given word?

For example, take the word BANANA.

It contains six characters. At first glance, we might think the total number of possible arrangements is simply 6!

But there is a catch!

The letter A appears three times, and the letter N appears twice.

So, if we simply calculate 6!, we end up counting several identical arrangements multiple times.



How can we calculate the number of unique permutations without counting duplicate arrangements?

What if we could do it using just one Excel formula?


Let's explore this interesting Excel formula!

1. The Excel Formula

Here is the formula:

=LET(
    nm,"Banana",
    l,LEN(nm),
    FACT(l)/PRODUCT(
        FACT(
            l-LEN(
                SUBSTITUTE(
                    nm,
                    UNIQUE(MID(nm,SEQUENCE(l),1)),
                )
            )
        )
    )
)

Yes! The word BANANA can be arranged in 60 distinct ways.

Let's understand how this formula works.


2. Understanding the Mathematics Behind It

The formula is based on a mathematical concept called permutations with repetition.

When all characters are different, the total number of possible arrangements is:

n!


For example, the word CAT contains three different letters.

The number of arrangements is:

3! = 3 x 2 x 1 = 6


But what happens when some characters repeat?

Consider BANANA:

  • Total characters = 6

  • B appears once.

  • A appears three times.

  • N appears twice.

The mathematical formula becomes:

= 6!/(3! x 2! x 1!)

= 720/12

= 60

This is exactly what our Excel formula calculates dynamically!


3. Breaking Down the Excel Formula


Now let's examine each function and understand its role.

Step 1: LET – Assign Variables

LET(nm,"Banana",l,LEN(nm),...)

The LET function allows us to assign names to intermediate calculations.

Here:

  • nm represents the input text.

  • l represents the length of the text.

For BANANA:

nm = "Banana"
l = 6

Using LET makes the formula easier to read and avoids repeating calculations.


Step 2: LEN – Calculate the Number of Characters

LEN(nm)

This returns the total number of characters in the given text.

For BANANA:

LEN("Banana") = 6

Therefore, our total number of arrangements starts with:

FACT(l)

Which gives:

6! = 720


Step 3: SEQUENCE – Generate Character Positions

SEQUENCE(l)

Since the length is 6, the result is:

{1;2;3;4;5;6}

This creates a sequence of positions that we can use to extract individual characters.


Step 4: MID – Extract Individual Characters

MID(nm,SEQUENCE(l),1)

The MID function extracts one character at each position.

For BANANA, the result is:

{"B";"a";"n";"a";"n";"a"}

Notice that we now have an array containing every character separately.


Step 5: UNIQUE – Identify Distinct Characters

UNIQUE(MID(nm,SEQUENCE(l),1))

This removes duplicate characters and returns only the distinct characters.

Result:

{"B";"a";"n"}

This is an important step because we need to calculate the frequency of each distinct character.


Step 6: SUBSTITUTE – Remove Each Character

Now comes the interesting part!

SUBSTITUTE(nm,UNIQUE(MID(nm,SEQUENCE(l),1)),"")

The SUBSTITUTE function removes each unique character from the original text.

Conceptually, the operation produces:

Character removed

Remaining text

Remaining length

B

anana

5

a

Bnn

3

n

Baaa

4

The result is an array of remaining text lengths:

{5;3;4}

Step 7: Calculate the Frequency of Each Character

Remember, the original text contains six characters.

So we subtract the remaining lengths from the original length:

l-LEN(SUBSTITUTE(...))

Result:

{6-5;6-3;6-4}

Which gives:

{1;3;2}

Fantastic!

We have now calculated the frequency of each distinct character.

Character

Frequency

B

1

a

3

n

2

Step 8: FACT – Calculate Factorials of Frequencies

Now we apply FACT to each frequency:

FACT(l-LEN(SUBSTITUTE(...)))

The resulting array is:

{1!;3!;2!}

Which evaluates to:

{1;6;2}

Step 9: PRODUCT – Multiply the Factorials

PRODUCT(FACT(...))

This multiplies all the factorial values:


= 1! x 3! x 2!

= 1 x 6 x 2

= 12

This gives us the denominator of the permutation formula.


Step 10: Calculate the Final Result

Finally:

FACT(l)/PRODUCT(FACT(...))

Substituting our values:

= 720/12

= 60

And there we have it!

Excel calculates the number of unique arrangements of BANANA using a single dynamic array formula.


4. Let's Test It with More Examples

The beauty of this formula is that we can replace the input text and calculate the result for different words.

Word

Characters

Unique permutations

CAT

3

6

APPLE

5

60

LEVEL

5

30

BANANA

6

60

GOOGLE

6

180

MISSISSIPPI

11

34,650

For example, consider APPLE:

  • Total characters = 5

  • A appears once.

  • P appears twice.

  • L appears once.

  • E appears once.

Therefore:

= 5!/(1! x 2! x 1! x 1!)

= 120/2

= 60

The formula handles all these repeated-character calculations automatically.


5. Why This Formula Is Interesting


There are several reasons why I find this formula interesting.

1. No helper columns required

Everything is calculated within one Excel formula.


2. No manual character counting

The UNIQUE, MID, and SUBSTITUTE combination automatically identifies distinct characters and calculates their frequencies.


3. Dynamic array functionality

SEQUENCE and UNIQUE make it possible to process all characters dynamically.


4. LET improves readability

Instead of repeating the input text and its length, we can assign variables and reuse them.


5. Mathematical logic meets Excel functions

This is a wonderful example of how a mathematical concept can be translated into a compact Excel formula.


6. A Few Important Considerations


  • Spaces and punctuation are also counted as characters.

  • The formula assumes the input is nonblank text.

  • For very long strings, factorial calculations may exceed Excel's numeric precision.

  • The formula requires a modern Excel version that supports LET, SEQUENCE, and UNIQUE.


7. Final Thoughts


Sometimes, Excel formulas are not just about calculations. They are about combining different functions creatively to solve interesting problems.

In this example, we have combined:

  • LET

  • LEN

  • FACT

  • PRODUCT

  • SUBSTITUTE

  • UNIQUE

  • MID

  • SEQUENCE


Eight Excel functions working together to solve a mathematical problem in a single formula!

The real magic is not just getting the answer. It's understanding the logic behind how the answer is calculated.


Happy Exceling! 😊


Recent Posts

See All

Comments


  • LinkedIn
  • Facebook
  • Twitter
  • Instagram
bottom of page