# LC 3127. Make a Square with the Same Color ### [Problem link](https://leetcode.com/problems/make-a-square-with-the-same-color/) ###### tags: `leedcode` `easy` `c++` You are given a 2D matrix <code>grid</code> of size <code>3 x 3</code> consisting only of characters <code>'B'</code> and <code>'W'</code>. Character <code>'W'</code> represents the white color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->, and character <code>'B'</code> represents the black color<!-- notionvc: 06a49cc0-a296-4bd2-9bfe-c8818edeb53a -->. Your task is to change the color of **at most one** cell<!-- notionvc: c04cb478-8dd5-49b1-80bb-727c6b1e0232 --> so that the matrix has a <code>2 x 2</code> square where all cells are of the same color.<!-- notionvc: adf957e1-fa0f-40e5-9a2e-933b95e276a7 --> Return <code>true</code> if it is possible to create a <code>2 x 2</code> square of the same color, otherwise, return <code>false</code>. **Example 1:** ![image](https://hackmd.io/_uploads/BkBPfwjb0.png) Input: <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","B"]] Output: <span class="example-io">true Explanation: It can be done by changing the color of the <code>grid[0][2]</code>. **Example 2:** ![image](https://hackmd.io/_uploads/Sy9tMPsWC.png) Input: <span class="example-io">grid = [["B","W","B"],["W","B","W"],["B","W","B"]] Output: <span class="example-io">false Explanation: It cannot be done by changing at most one cell. **Example 3:** ![image](https://hackmd.io/_uploads/S1O9fwjW0.png) Input: <span class="example-io">grid = [["B","W","B"],["B","W","W"],["B","W","W"]] Output: <span class="example-io">true Explanation: The <code>grid</code> already contains a <code>2 x 2</code> square of the same color.<!-- notionvc: 9a8b2d3d-1e73-457a-abe0-c16af51ad5c2 --> **Constraints:** - <code>grid.length == 3</code> - <code>grid[i].length == 3</code> - <code>grid[i][j]</code> is either <code>'W'</code> or <code>'B'</code>. ## Solution 1 #### C++ ```cpp= class Solution { public: bool canMakeSquare(vector<vector<char>>& grid) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { unordered_map<char, int> umap; umap[grid[i][j]]++; umap[grid[i][j + 1]]++; umap[grid[i + 1][j]]++; umap[grid[i + 1][j + 1]]++; if (umap['B'] != 2 && umap['W'] != 2) { return true; } } } return false; } }; ``` >### Complexity >n = grid.length >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n^2) | O(1) | ## Note x [](https://)