FastPrepPassword From Ordering Hints

Password From Ordering Hints

Rogo logoRogo● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Each string in hints lists distinct password characters in their required relative order. The password contains every distinct character that appears in any hint exactly once.

Return the lexicographically smallest password consistent with every hint. Return the empty string when the hints contain a cycle and no valid password exists.

Function

deducePassword(hints: String[]) → String

Examples

Example 1

hints = ["wrt","wrf","er","ett","rftt"]return = ""

Combining adjacent constraints yields the unique order wertf.

Example 2

hints = ["ab","ba"]return = ""

The two hints require both a before b and b before a.

Constraints

  • 1 <= hints.length <= 10000
  • Each hint contains 1 to 62 distinct letters or digits.
  • At most 62 distinct characters appear overall.
See Rogo hiring insights
public String deducePassword(String[] hints) {
  // Write your code here.
}
hints["wrt","wrf","er","ett","rftt"]
expected""
Checking account…