Where I live, there is a keypad with numbers from 0 to 9 and two letters, A and B.Example of keypad like mine

Using it I noticed that the code was checked on the last 5 things entered. If I were to try every code on the keypad, I simple answer would be to start from 00000,00001,00002... BBBBB.

However, that would mean that time was wasted in the process. Consider someone typing 123456789 on the keypad.

In this case, if the door doesn't open, we know that the following codes are invalid:

  • 12345
  • 23456
  • 34567
  • 45678
  • 56789

What I'm looking for is an algorithm, or at least the name of, to find the path with the fewer key presses.

I'm not looking for a "clever way" of guessing the code, as in look at what digits are dirty.


This is called a De Bruijn sequence. A pattern that contains all patterns within it.

| improve this answer | |
  • 1
    Exactly what I was looking for, thanks. For any future reader, there is a python function you can run to get the full sequence for whatever alphabet and code size! – WayToDoor Dec 17 '18 at 12:48
  • 1
    Video about this: This Toy Can Open Any Garage – Sjoerd Dec 17 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.