1169. Invalid Transactions LeetCode Solution
In this guide, you will get 1169. Invalid Transactions LeetCode Solution with the best time and space complexity. The solution to Invalid Transactions problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Invalid Transactions solution in C++
- Invalid Transactions solution in Java
- Invalid Transactions solution in Python
- Additional Resources

Problem Statement of Invalid Transactions
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.
Example 1:
Input: transactions = [“alice,20,800,mtv”,”alice,50,100,beijing”]
Output: [“alice,20,800,mtv”,”alice,50,100,beijing”]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = [“alice,20,800,mtv”,”alice,50,1200,mtv”]
Output: [“alice,50,1200,mtv”]
Example 3:
Input: transactions = [“alice,20,800,mtv”,”bob,50,1200,mtv”]
Output: [“bob,50,1200,mtv”]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000.
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
1169. Invalid Transactions LeetCode Solution in C++
struct Transaction {
string name;
int time;
int amount;
string city;
};
class Solution {
public:
vector<string> invalidTransactions(vector<string>& transactions) {
vector<string> ans;
unordered_map<string, vector<Transaction>> nameToTransactions;
for (const string& t : transactions) {
const Transaction transaction = getTransaction(t);
nameToTransactions[transaction.name].push_back(transaction);
}
for (const string& t : transactions) {
const Transaction currTransaction = getTransaction(t);
if (currTransaction.amount > 1000) {
ans.push_back(t);
} else if (const auto it = nameToTransactions.find(currTransaction.name);
it != nameToTransactions.cend()) {
// Iterate through all the transactions with the same name, check if
// they're within 60 minutes in a different city.
for (Transaction transaction : it->second)
if (abs(transaction.time - currTransaction.time) <= 60 &&
transaction.city != currTransaction.city) {
ans.push_back(t);
break;
}
}
}
return ans;
}
private:
Transaction getTransaction(const string& t) {
istringstream iss(t);
vector<string> s(4, "");
for (int i = 0; getline(iss, s[i++], ',');)
;
return {s[0], stoi(s[1]), stoi(s[2]), s[3]}; // [name, time, amount, city]
}
};
/* code provided by PROGIEZ */
1169. Invalid Transactions LeetCode Solution in Java
class Solution {
public List<String> invalidTransactions(String[] transactions) {
List<String> ans = new ArrayList<>();
Map<String, List<Trans>> nameToTrans = new HashMap<>();
for (final String t : transactions) {
Trans trans = getTrans(t);
nameToTrans.putIfAbsent(trans.name, new ArrayList<>());
nameToTrans.get(trans.name).add(trans);
}
for (final String t : transactions) {
Trans currTrans = getTrans(t);
if (currTrans.amount > 1000) {
ans.add(t);
} else if (nameToTrans.containsKey(currTrans.name)) {
// Iterate through all the transactions with the same name, check if
// they're within 60 minutes in a different city.
for (Trans trans : nameToTrans.get(currTrans.name))
if (Math.abs(trans.time - currTrans.time) <= 60 && !trans.city.equals(currTrans.city)) {
ans.add(t);
break;
}
}
}
return ans;
}
private record Trans(String name, int time, int amount, String city) {}
private Trans getTrans(final String t) {
String[] s = t.split(","); // [name, time, amount, city]
return new Trans(s[0], Integer.parseInt(s[1]), Integer.parseInt(s[2]), s[3]);
}
}
// code provided by PROGIEZ
1169. Invalid Transactions LeetCode Solution in Python
class Solution:
def invalidTransactions(self, transactions: list[str]) -> list[str]:
ans = []
nameToTrans = collections.defaultdict(list)
for t in transactions:
name, time, amount, city = t.split(',')
time, amount = int(time), int(amount)
nameToTrans[name].append({'time': time, 'city': city})
for t in transactions:
name, time, amount, city = t.split(',')
time, amount = int(time), int(amount)
if amount > 1000:
ans.append(t)
elif name in nameToTrans:
for sameName in nameToTrans[name]:
if abs(sameName['time'] - time) <= 60 and sameName['city'] != city:
ans.append(t)
break
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.