The Most Similar Path in a Graph - Problem

We have n cities and m bi-directional roads where roads[i] = [ai, bi] connects city ai with city bi. Each city has a name consisting of exactly three upper-case English letters given in the string array names.

Starting at any city x, you can reach any city y where y != x (i.e., the cities and the roads are forming an undirected connected graph).

You will be given a string array targetPath. You should find a path in the graph of the same length and with the minimum edit distance to targetPath.

You need to return the order of the nodes in the path with the minimum edit distance. The path should be of the same length of targetPath and should be valid (i.e., there should be a direct road between ans[i] and ans[i + 1]).

If there are multiple answers return any one of them.

The edit distance is defined as follows:

  • Edit distance is the number of positions where the path differs from the target path
  • Lower edit distance means the path is more similar to the target

Input & Output

Example 1 — Basic Graph Path
$ Input: n = 5, roads = [[0,1],[1,2],[2,3],[3,4],[1,4]], names = ["AAA","BBB","CCC","DDD","EEE"], targetPath = ["AAA","CCC"]
Output: [0,2]
💡 Note: Path [0,2] gives ["AAA","CCC"] with edit distance 0 (perfect match). Other paths like [0,1] would give ["AAA","BBB"] with edit distance 1.
Example 2 — Minimum Edit Distance
$ Input: n = 4, roads = [[0,1],[1,2],[2,3],[0,3]], names = ["ATL","PEK","LAX","DXB"], targetPath = ["ABC","DEF","GHI"]
Output: [0,1,2]
💡 Note: No perfect match exists. Path [0,1,2] gives ["ATL","PEK","LAX"] with edit distance 3 (all positions differ from ["ABC","DEF","GHI"]).
Example 3 — Single City Path
$ Input: n = 2, roads = [[0,1]], names = ["AAA","BBB"], targetPath = ["BBB"]
Output: [1]
💡 Note: Target has length 1. City 1 has name "BBB" which exactly matches targetPath[0], giving edit distance 0.

Constraints

  • 2 ≤ n ≤ 100
  • n - 1 ≤ roads.length ≤ n × (n - 1) / 2
  • roads[i].length == 2
  • 0 ≤ ai, bi ≤ n - 1
  • ai ≠ bi
  • names.length == n
  • names[i].length == 3
  • names[i] consists of uppercase English letters
  • 1 ≤ targetPath.length ≤ 100
  • targetPath[i].length == 3
  • targetPath[i] consists of uppercase English letters

Visualization

Tap to expand
Find Most Similar Path: Graph → Target → Best PathInput Graph012AAABBBCCCTarget Path[AAA, CCC]Best Path Found02AAACCCPath [0,2]: AAA → CCCEdit Distance: 0 (Perfect Match!)Algorithm: Use DP to find path with minimum differencesdp[position][city] = minimum edit distance to reach city at positionResult: [0, 2] with edit distance 0
Understanding the Visualization
1
Input Graph
Cities connected by roads with 3-letter names
2
Target Path
Sequence of city names we want to match
3
Find Best Path
Valid path with minimum edit distance to target
Key Takeaway
🎯 Key Insight: Use dynamic programming to efficiently explore all valid paths and track minimum edit distance at each step
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
23.4K Views
Medium Frequency
~35 min Avg. Time
542 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen