compsci_guides icon indicating copy to clipboard operation
compsci_guides copied to clipboard

Solution provided doesn't even work for Marking the Event Timeline

Open edisonfreire opened this issue 11 months ago • 0 comments

https://github.com/codepath/compsci_guides/wiki/Marking-the-Event-Timeline

This is a solution, I was able to come up with that actually worked using the BFS approach mentioned but the time complexity is kind of busted.

def mark_event_timeline(event: str, timeline: str):
    n = len(timeline)
    max_turns = n * 10
    t = '?' * n
    queue = deque([(t, [])])
    seen = set(t)
    while queue:
        curr_t, curr_path = queue.popleft()

        if len(curr_path) >= max_turns:
            continue

        if curr_t == timeline:
            return curr_path

        for i in range(0, n - len(event) + 1):
            if curr_t[i: i + len(event)] == event:
                continue  # no change
            new_t = curr_t[:i] + event + curr_t[i + len(event):]
            if new_t not in seen:
                queue.append((new_t, curr_path + [i]))
                seen.add(new_t)

    return []

edisonfreire avatar Feb 27 '25 23:02 edisonfreire