Skip to content

Commit b3b46dc

Browse files
solves #3127: Make a Square with the Same Color in java
1 parent cea4929 commit b3b46dc

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@
906906
| 3110 | [Score of a String](https://leetcode.com/problems/score-of-a-string) | [![Java](assets/java.png)](src/ScoreOfAString.java) | |
907907
| 3114 | [Latest Time You Can Obtain After Replacing Characters](https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters) | [![Java](assets/java.png)](src/LatestTimeYouCanObtainAfterReplacingCharacters.java) | |
908908
| 3120 | [Count the Number of Special Characters I](https://leetcode.com/problems/count-the-number-of-special-characters-i) | [![Java](assets/java.png)](src/CountTheNumberOfSpecialCharactersI.java) | |
909-
| 3127 | [Make a Square with the Same Color](https://leetcode.com/problems/make-a-square-with-the-same-color) | | |
909+
| 3127 | [Make a Square with the Same Color](https://leetcode.com/problems/make-a-square-with-the-same-color) | [![Java](assets/java.png)](src/MakeASquareWithTheSameColor.java) | |
910910
| 3131 | [Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i) | | |
911911
| 3136 | [Valid Word](https://leetcode.com/problems/valid-word) | | |
912912
| 3142 | [Check if Grid Satisfies Conditions](https://leetcode.com/problems/check-if-grid-satisfies-conditions) | | |

src/MakeASquareWithTheSameColor.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// https://leetcode.com/problems/make-a-square-with-the-same-color
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class MakeASquareWithTheSameColor {
6+
public boolean canMakeSquare(char[][] grid) {
7+
for (int i = 0 ; i < 2 ; i++) {
8+
for (int j = 0 ; j < 2 ; j++) {
9+
if (isPossibleToMakeSquare(grid, i, j)) {
10+
return true;
11+
}
12+
}
13+
}
14+
return false;
15+
}
16+
17+
private static boolean isPossibleToMakeSquare(char[][] grid, int i, int j) {
18+
int numberOfBlacks = 0;
19+
int numberOfWhites = 0;
20+
21+
for (int row = i ; row < i + 2 ; row++) {
22+
for (int column = j ; column < j + 2 ; column++) {
23+
if (grid[row][column] == 'W') {
24+
numberOfWhites++;
25+
} else {
26+
numberOfBlacks++;
27+
}
28+
}
29+
}
30+
31+
return numberOfBlacks == 4 || numberOfWhites == 4 || numberOfBlacks == 3 || numberOfWhites == 3;
32+
}
33+
}

0 commit comments

Comments
 (0)