Shared Travel Order
Medium · 2-D & String DP · about 25 min
Two friends wrote down the cities they visited this year, one letter per city, in the order they visited them: strings a and b. They want the longest list of cities that both visited in the same order. The cities in the list do not have to be next to each other in either string.
Return the length of that longest shared list.
0 <= len(a), len(b) <= 1000, lowercase letters only.
Examples:
shared_order_length("abcde", "ace") → 3 (a, c, e)
shared_order_length("abc", "cba") → 1
Hints
- Compare the last letters of the two prefixes.
- If they match, both can be the last city of the shared list. If not, at least one of them is not in it.
- Build a table with one extra row and column of zeros for the empty prefixes.
Learn the pattern first: the 2-D & String DP lesson.
More 2-D & String DP problems
- Warehouse Robot Routes (Easy)
- Lightest Hiking Route (Easy)
- Longest Mirror Piece (Medium)
- Two Equal Delivery Vans (Medium)
- Plus or Minus Ledger (Medium)
- Fill the Cargo Bay Exactly (Medium)
- Ways to Pay the Toll (Medium)
- Priced Spelling Fix (Hard)
- Hidden Copies in a Log (Hard)
- File Filter Wildcards (Hard)
- Label Printer Tiles (Hard)