<p>Given an array <code>rectangles</code> where <code>rectangles[i] = [x<sub>i</sub>, y<sub>i</sub>, a<sub>i</sub>, b<sub>i</sub>]</code> represents an axis-aligned rectangle. The bottom-left point of the rectangle is <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and the top-right point of it is <code>(a<sub>i</sub>, b<sub>i</sub>)</code>.</p>

<p>Return <code>true</code> <em>if all the rectangles together form an exact cover of a rectangular region</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/perectrec1-plane.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
<strong>Output:</strong> true
<strong>Explanation:</strong> All 5 rectangles together form an exact cover of a rectangular region.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/perfectrec2-plane.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
<strong>Output:</strong> false
<strong>Explanation:</strong> Because there is a gap between the two rectangular regions.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/perfecrrec4-plane.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]
<strong>Output:</strong> false
<strong>Explanation:</strong> Because two of the rectangles overlap with each other.
</pre>

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

<ul>
	<li><code>1 &lt;= rectangles.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>rectangles[i].length == 4</code></li>
	<li><code>-10<sup>5</sup> &lt;= x<sub>i</sub> &lt; a<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= y<sub>i</sub> &lt; b<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    bool isRectangleCover(vector<vector<int>>& rectangles) {
    int n = rectangles.size();
    if (n == 1) {
        return true;
    }
    unordered_set<string> S;
    S.insert(to_string(rectangles[0][0])+","+ to_string(rectangles[0][1]));
    S.insert(to_string(rectangles[0][0])+","+ to_string(rectangles[0][3]));
    S.insert(to_string(rectangles[0][2])+","+ to_string(rectangles[0][1]));
    S.insert(to_string(rectangles[0][2])+","+ to_string(rectangles[0][3]));

    int MinX=rectangles[0][0], MaxX=rectangles[0][2], MinY=rectangles[0][1], MaxY=rectangles[0][3];
    int areas=(MaxX-MinX)*(MaxY-MinY);

    for (int i = 1; i < n; ++i) {
        if (MinX>rectangles[i][0]){
            MinX=rectangles[i][0];
        }

        if (MaxX<rectangles[i][2]){
            MaxX=rectangles[i][2];
        }

        if (MinY>rectangles[i][1]){
            MinY=rectangles[i][1];
        }

        if (MaxY<rectangles[i][3]){
            MaxY=rectangles[i][3];
        }

        areas+=((rectangles[i][2]-rectangles[i][0])*(rectangles[i][3]-rectangles[i][1]));

        vector<string> rect={to_string(rectangles[i][0])+","+ to_string(rectangles[i][1]), to_string(rectangles[i][0])+","+ to_string(rectangles[i][3]), to_string(rectangles[i][2])+","+ to_string(rectangles[i][1]), to_string(rectangles[i][2])+","+ to_string(rectangles[i][3])};

        for (auto el:rect) {
            if (S.count(el)){
                S.erase(el);
            }
            else{
                S.insert(el);
            }
        }
    }

    int A=MaxX-MinX ,B=MaxY-MinY;
    int Area=A*B;

    if (areas!=Area || S.size()!=4){
        return false;
    }

    vector<string> Rect={to_string(MinX)+","+ to_string(MinY), to_string(MinX)+","+ to_string(MaxY), to_string(MaxX)+","+ to_string(MinY), to_string(MaxX)+","+ to_string(MaxY)};

    for (int i = 0; i < 4; ++i) {
        if (S.count(Rect[i])==0){
            return false;
        }
    }

    return true;
}
};
48300