You are given a list of vacation rental listings. Each listing has a unique string id and a sorted array of available days (integers representing calendar days).
Two listings form a valid split-stay pair if one listing's last available day is immediately followed by the other listing's first available day. In other words, if listing A's last day is d and listing B's first day is d + 1, then travelers can stay at listing A and then continue their stay at listing B with no gap.
Return all valid split-stay pairs as [id1, id2] where id1 < id2 (lexicographically), sorted in lexicographic order. A pair (A, B) is valid if either A's last day + 1 equals B's first day, or B's last day + 1 equals A's first day.
Function Signature
def split_stay_pairs(listings: list[list]) -> list[list[str]]:
Rules
-
Each listing is given as
[id, days]
, for example
["A", [1, 2, 3]]
.
-
Inside a pair the smaller id comes first, whichever listing is stayed in first.
-
Ids compare by ordinary string comparison (character code order, so
"B" < "a"
).
-
Sort the pairs by first id, then by second id. Return an empty list when no pair exists.
Constraints
-
1 <= len(listings) <= 1000
-
Each listing has a unique string
id
and a non-empty sorted array of day integers
-
1 <= len(days) <= 10^4
-
1 <= days[i] <= 10^6
-
Days within each listing are sorted in strictly increasing order
Examples
Example 1
Input: listings = [["A", [1, 2, 3]], ["B", [4, 5, 6]], ["C", [7, 8, 9]]]
Output: [["A", "B"], ["B", "C"]]
Listing A ends on day 3 and listing B starts on day 4, so (A, B) is a valid pair. Listing B ends on day 6 and listing C starts on day 7, so (B, C) is a valid pair. Listing A does not connect directly to C.
Example 2
Input: listings = [["X", [1, 2, 3]], ["Y", [3, 4, 5]], ["Z", [6, 7, 8]]]
Output: [["Y", "Z"]]
Listing X ends on day 3, so it would need a listing that starts on day 4. Y starts on day 3 and does not qualify. Listing Y ends on day 5 and listing Z starts on day 6, so (Y, Z) is a valid pair.
Example 3
Input: listings = [["d", [10, 11]], ["b", [1, 2]], ["a", [3, 4, 5]], ["c", [3, 9]]]
Output: [["a", "b"], ["b", "c"], ["c", "d"]]
Listing b ends on day 2 and both a and c start on day 3, which gives two pairs. Listing c ends on day 9 and d starts on day 10. The first pair is written ["a", "b"] even though the stay at b comes first, because ids inside a pair are ordered by id.