We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is how I solve this challenge. I uses a topological sorting approach to recover the shortest possible secret passcode based on the given login attempts. How it works:
Character Set: First, it extracts all unique characters from the login attempts and stores them in the chars set.
Graph Construction: It constructs a directed graph where each character is a node, and there is a directed edge from character a to character b if a comes before b in any login attempt.
In-Degree Calculation: It calculates the in-degree of each node, representing the number of characters that should come before it.
Topological Sorting: It performs a topological sort on the graph. Starting with characters that have an in-degree of zero, it adds them to the result in lexicographically sorted order. It updates in-degrees and continues the process until all characters are processed.
Result and Validity Check: The final result is the lexicographically smallest order of characters that satisfies the given login attempts. It also checks if there is a unique topological order. If any character has a non-zero in-degree at the end, it indicates an issue, and the result is "SMTH WRONG."
This approach ensures that the recovered passcode is the shortest and lexicographically smallest possible, and it handles cases where there is ambiguity or inconsistencies in the login attempts.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #79: Passcode derivation
You are viewing a single comment's thread. Return to all comments →
This is how I solve this challenge. I uses a topological sorting approach to recover the shortest possible secret passcode based on the given login attempts. How it works:
Character Set: First, it extracts all unique characters from the login attempts and stores them in the chars set.
Graph Construction: It constructs a directed graph where each character is a node, and there is a directed edge from character a to character b if a comes before b in any login attempt.
In-Degree Calculation: It calculates the in-degree of each node, representing the number of characters that should come before it.
Topological Sorting: It performs a topological sort on the graph. Starting with characters that have an in-degree of zero, it adds them to the result in lexicographically sorted order. It updates in-degrees and continues the process until all characters are processed.
Result and Validity Check: The final result is the lexicographically smallest order of characters that satisfies the given login attempts. It also checks if there is a unique topological order. If any character has a non-zero in-degree at the end, it indicates an issue, and the result is "SMTH WRONG."
This approach ensures that the recovered passcode is the shortest and lexicographically smallest possible, and it handles cases where there is ambiguity or inconsistencies in the login attempts.