Password Cracker

Solving the Programming Challenge
Difficulty: Medium Source: Hacker Rank
Feature Image

Problem

There are N users registered on a website CuteKittens.com. Each of them has a unique password represented by pass[1], pass[2], ..., pass[N]. As this a very lovely site, many people want to access those awesomely cute pics of the kittens. But the adamant admin does not want the site to be available to the general public, so only those people who have passwords can access it.

Yu, being an awesome hacker finds a loophole in the password verification system. A string which is a concatenation of one or more passwords, in any order, is also accepted by the password verification system. Any password can appear 0 or more times in that string. Given access to each of the n passwords, and also have a string loginAttempt,determinewhetherthisstringbeacceptedbythepasswordverificationsystemofthewebsite loginAttempt$ string can be created by concatenating password strings, it is accepted.

For example, if there are 3 users with passwords = [abra, ka, dabra], then some of the valid combinations are abra (password[1]), kaabra (password[2] + password[1]), kadabraka (password[2] + password[3] + password[2]), kadabraabra (password[2] + password[3] + password[1]) and so on. Supplying abra ka dabra, concatenated, passes authentication.

Function Description

Complete the passwordCracker function in the editor below. It should return the passwords as a single string in the order required for the password to be accepted, each separated by a space. If it is not possible to form the string, return the string WRONG PASSWORD.

passwordCracker has the following parameters:

  • passwords: a list of password strings
  • loginAttempt: the string to attempt to create

Read more on the challenge page…


Problem Analysis and Solution


My Solution

I’m providing the solution for Python and JS, please leave on the comments if you found a better way.