Problem Description
Explanation
To determine if two squares on a chessboard have the same color, we can observe that squares of the same color always have coordinates with either both odd or both even sums. We can convert the chessboard coordinates into numeric values to calculate the sum of row and column indices. By comparing the sums of the two coordinates, we can determine if the squares have the same color.
- Time Complexity: O(1) as the algorithm has a constant time complexity.
- Space Complexity: O(1) as we are not using any extra space.
Solutions
class Solution {
public boolean squareIsWhite(String coordinates) {
int sum = coordinates.charAt(0) + coordinates.charAt(1);
return sum % 2 == 0;
}
}
Loading editor...