Be the first user to complete this post

  • 0
Add to List
Medium

167. Generate Well Ordered Passwords of a Given Length K

Objective: Generate Well-Ordered Passwords of a Given Length K. Well-ordered means that digits should be in increasing order in every generated password.

Example:

K = 7

1234567 1234568 1234569 1234578 1234579 1234589 1234678 1234679 1234689 1234789 1235678 1235679 1235689 1235789 1236789 1245678 1245679 1245689 1245789 1246789 1256789 1345678 1345679 1345689 1345789 1346789 1356789 1456789 2345678 2345679 2345689 2345789 2346789 2356789 2456789 3456789

Approach:

This problem is quite similar to Print All N Length Strings from Given Number K.

  • Loop through i = x to 9. ( all digits will be from 1 to 9).
  • x will start with 0 and will be incremented with every recursive call to make sure that password will be well formed.
  • With every recursive call, multiply the result (which will start with 0) by 10 and add I, and make k = k-1.
  • Base case: when k becomes 0 (means the array is full).
  • See the code for a better explanation.

Output:

1234567 1234568 1234569 1234578 1234579 1234589
1234678 1234679 1234689 1234789 1235678 1235679
1235689 1235789 1236789 1245678 1245679 1245689
1245789 1246789 1256789 1345678 1345679 1345689
1345789 1346789 1356789 1456789 2345678 2345679
2345689 2345789 2346789 2356789 2456789 3456789