Given a stream of transaction data, identify anomalous patterns or duplicate entries within a rolling time window.
Implement detect_anomalies(transactions, window) for transactions ordered by nondecreasing timestamp. Each transaction is a dictionary with timestamp, transaction_id, user_id, and amount. Return {"duplicate_indices": [...], "anomalous_indices": [...]}. A transaction is a duplicate if its ID appeared within the previous window seconds. It is anomalous if it is the third or later transaction for the same user within that window. Indices refer to the original input list.
def detect_anomalies(transactions, window):