<p>A transaction is possibly invalid if:</p>

<ul>
	<li>the amount exceeds <code>$1000</code>, or;</li>
	<li>if it occurs within (and including) <code>60</code> minutes of another transaction with the <strong>same name</strong> in a <strong>different city</strong>.</li>
</ul>

<p>You are given an array of strings <code>transaction</code> where <code>transactions[i]</code> consists of comma-separated values representing the name, time (in minutes), amount, and city of the transaction.</p>

<p>Return a list of <code>transactions</code> that are possibly invalid. You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> transactions = [&quot;alice,20,800,mtv&quot;,&quot;alice,50,100,beijing&quot;]
<strong>Output:</strong> [&quot;alice,20,800,mtv&quot;,&quot;alice,50,100,beijing&quot;]
<strong>Explanation:</strong> 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.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> transactions = [&quot;alice,20,800,mtv&quot;,&quot;alice,50,1200,mtv&quot;]
<strong>Output:</strong> [&quot;alice,50,1200,mtv&quot;]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> transactions = [&quot;alice,20,800,mtv&quot;,&quot;bob,50,1200,mtv&quot;]
<strong>Output:</strong> [&quot;bob,50,1200,mtv&quot;]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>transactions.length &lt;= 1000</code></li>
	<li>Each <code>transactions[i]</code> takes the form <code>&quot;{name},{time},{amount},{city}&quot;</code></li>
	<li>Each <code>{name}</code> and <code>{city}</code> consist of lowercase English letters, and have lengths between <code>1</code> and <code>10</code>.</li>
	<li>Each <code>{time}</code> consist of digits, and represent an integer between <code>0</code> and <code>1000</code>.</li>
	<li>Each <code>{amount}</code> consist of digits, and represent an integer between <code>0</code> and <code>2000</code>.</li>
</ul>

class Solution {
public:
    
    vector<string> invalidTransactions(vector<string>& transactions) {
        vector<string> res;
        // st is for invalid transactions
        unordered_set<string> st;
        // m is the dict of customer's name and transactions
        unordered_map<string, vector<vector<string>>> m;
        unordered_map<string, int> cntMap;
        
        for (string t : transactions) {
            ++cntMap[t];
            istringstream iss(t);
            int i = 0;
            // name, time (in minutes), amount, city
            vector<string> vec(4);
            // istream& getline (istream& is, string& str, char delim); 
            // reads the input string stream iss and extracts the elements separated by comma (',') into the vector vec.
            // The while loop continues to execute as long as the getline operation succeeds, i.e., it does not reach the end of the input stream.
            while (getline(iss, vec[i++], ',')); 
            
            // over 1000 dollars, insert
            if (stoi(vec[2]) > 1000) 
                st.insert(t);
            
            for (auto &a : m[vec[0]]) {
                if (a[3] != vec[3] && abs(stoi(a[1]) - stoi(vec[1])) <= 60) {
                    st.insert(a[0] + "," + a[1] + "," + a[2] + "," + a[3]);
                    if (!st.count(t))
                        st.insert(t);
                }
            }
            m[vec[0]].push_back(vec);
        }
        
        for (auto &a : cntMap) {
            if (st.count(a.first)) {
                for (int i = 0; i < a.second - 1; ++i) {
                    res.push_back(a.first);
                }
            }
        }
        res.insert(res.end(), st.begin(), st.end());
        return res;
    }
};




// class Solution {
// public:
//     vector<string> invalidTransactions(vector<string>& transactions) {
//         unordered_set< string > res;
//         unordered_map< string, vector< vector< string > > > m;
//         for (auto &t: transactions) {
//             istringstream ss(t);
//             // creates a vector of strings named s with 4 elements, each initialized to an empty string "".
//             vector<string> s(4, "");
//             int i = 0;
//             // The code while(getline(ss, s[i++], ',')); reads the input string stream ss and extracts the elements separated by comma (',') into the vector s.
//             // getline(ss, s[i++], ',') reads the next element from the input stream ss until it encounters the delimiter ,. The extracted element is then stored in the i-th position of the vector s. The i++ expression increments the value of i after this operation.
//             // The while loop continues to execute as long as the getline operation succeeds, i.e., it does not reach the end of the input stream.
//             while(getline(ss, s[i++], ','));
//             // stoi() is a function that converts a string to an integer
//             if (stoi(s[2]) > 1000) res.insert(t);
            
//             for(vector<string> j:m[s[0]])
//                 if ((j[3] != s[3]) && abs( stoi(j[1])-stoi(s[1]) <= 60 )){
//                     res.insert( j[0]+","+j[1]+","+j[3] );
//                     if(!res.count(t)) res.insert(t);
//                     // res.insert(t);
//                 }
//             m[s[0]].push_back({s[0],s[1],s[2],s[3]});
            
//         }
//         vector<string> ans;
//         for (auto &k: res) ans.push_back(k);
//         return ans;
        
//     }
    
    
// };









///////






////////


24400