Fare Between Stops on a Train Route
Problem statement
A train route is given as an ordered array of unique stop names. Traveling across one adjacent segment costs one fare unit in either direction.
Implement calculateFare to return the fare between start and stop. Return -1 if either requested stop is absent.
Function
calculateFare(route: String[], start: String, stop: String) → intExamples
Example 1
route = ["G","U","H","K","I"]start = "G"stop = "I"return = 4The endpoints are four adjacent segments apart.
Example 2
route = ["A","B","C"]start = "C"stop = "A"return = 2Travel in the reverse direction costs the same two units.
Example 3
route = ["A","B"]start = "A"stop = "Z"return = -1Z is not a stop on the route.
Constraints
1 ≤ route.length ≤ 100000.- Every route entry is a non-empty unique printable ASCII string.
startandstopare non-empty printable ASCII strings.