1272. Invalid Transactions
My accepted Python solution to LeetCode problem 1272, Invalid Transactions, running in 50ms.
- Difficulty: Medium
- Python
- Runtime 50ms
- Memory 18.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 50ms, memory 18.3MB, accepted 2026-01-02.
class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
from collections import defaultdict
# Parse transactions
parsed = []
for i, t in enumerate(transactions):
name, time, amount, city = t.split(',')
parsed.append((name, int(time), int(amount), city, i))
invalid = set()
# Check each transaction
name_transactions = defaultdict(list)
for name, time, amount, city, idx in parsed:
name_transactions[name].append((time, city, idx))
for name, time, amount, city, idx in parsed:
# Invalid if amount > 1000
if amount > 1000:
invalid.add(idx)
# Check for transactions with same name in different city within 60 minutes
for other_time, other_city, other_idx in name_transactions[name]:
if city != other_city and abs(time - other_time) <= 60:
invalid.add(idx)
invalid.add(other_idx)
return [transactions[i] for i in invalid]