Problem · Array
Wormhole Spacecraft Overtakes
Learn this problemProblem statement
Distinct spacecraft enter a wormhole one at a time in entryOrder and emerge in exitOrder. Both arrays contain the same identifiers exactly once.
Two spacecraft crossed if the one that entered later emerged earlier. Return the number of distinct spacecraft that crossed at least one other spacecraft. Count each spacecraft at most once.
Function
countCrossingSpacecraft(entryOrder: String[], exitOrder: String[]) → intExamples
Example 1
entryOrder = ["A","B","C","D"]exitOrder = ["A","C","B","D"]return = 2B entered before C but emerged after it, so exactly those two spacecraft participated in a crossing.
Example 2
entryOrder = ["A","B","C"]exitOrder = ["C","B","A"]return = 3Every spacecraft belongs to at least one inverted pair.
Constraints
1 <= entryOrder.length = exitOrder.length <= 200000.- Each identifier contains 1 to 20 ASCII letters, digits, or underscores.
- Every identifier occurs exactly once in each array.