Skip to main content
Back to problems
#1169
Medium Algorithms

Invalid transactions

Array Hash Table String Sorting
32.1% acceptance
Feb 25, 2026
611
2430
A transaction is possibly invalid if: the amount exceeds $1000, or; if it occurs within (and including) 60 minutes of another transaction with the same name in a different city. You are given an array of strings transaction where transactions[i] consists of comma-separated values representing the name, time (in minutes), amount, and city of the transaction. Return a list of transactions that are possibly invalid. You may return the answer in any order.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn invalid_transactions(transactions: Vec<String>) -> Vec<String> {
    let parsed: Vec<(&str, i32, i32, &str)> = transactions.iter().map(|t| {
      let p: Vec<&str> = t.splitn(4, ',').collect();
      (p[0], p[1].parse().unwrap(), p[2].parse().unwrap(), p[3])
    }).collect();
    let mut invalid = vec![false; parsed.len()];
    for i in 0..parsed.len() {
      if parsed[i].2 > 1000 { invalid[i] = true; }
      for j in 0..parsed.len() {
        if i != j && parsed[i].0 == parsed[j].0
          && parsed[i].3 != parsed[j].3
          && (parsed[i].1 - parsed[j].1).abs() <= 60 {
          invalid[i] = true;
          break;
        }
      }
    }
    transactions.into_iter().enumerate()
      .filter(|(i, _)| invalid[*i])
      .map(|(_, t)| t)
      .collect()
  }
}