
Input: points =[[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]], s ="abdca"Output: 2Explanation:
The square of side length 4 covers two points `points[0]` and `points[1]`.

Input: points =[[1,1],[-2,-2],[-2,2]], s ="abb"Output: 1Explanation:
The square of side length 2 covers one point, which is`points[0]`.
Input: points =[[1,1],[-1,-1],[2,-2]], s ="ccd"Output: 0Explanation:
It's impossible to make any valid squares centered at the origin such that it
covers only one point among `points[0]` and `points[1]`.
We want to find the largest square (centered at the origin) that contains the maximum number of points, with the restriction that no two points inside the square have the same tag. Since the square is axis-aligned and centered at (0,0), we can use binary search on the possible side length, and for each candidate, check if it’s possible to select points with unique tags inside the square.
classSolution {
public:int maxPointsInsideSquare(vector<vector<int>>& points, string s) {
int n = points.size();
int l =0, r =0, ans =0;
for (auto& p : points) r = max(r, max(abs(p[0]), abs(p[1])));
while (l <= r) {
int d = (l + r) /2;
unordered_set<char> tags;
int cnt =0;
bool valid = true;
for (int i =0; i < n; ++i) {
if (abs(points[i][0]) <= d && abs(points[i][1]) <= d) {
if (tags.count(s[i])) { valid = false; break; }
tags.insert(s[i]);
cnt++;
}
}
if (valid) {
ans = max(ans, cnt);
l = d +1;
} else {
r = d -1;
}
}
return ans;
}
};
classSolution {
publicintmaxPointsInsideSquare(int[][] points, String s) {
int n = points.length, l = 0, r = 0, ans = 0;
for (int[] p : points) r = Math.max(r, Math.max(Math.abs(p[0]), Math.abs(p[1])));
while (l <= r) {
int d = (l + r) / 2;
Set<Character> tags =new HashSet<>();
int cnt = 0;
boolean valid =true;
for (int i = 0; i < n; ++i) {
if (Math.abs(points[i][0]) <= d && Math.abs(points[i][1]) <= d) {
if (tags.contains(s.charAt(i))) { valid =false; break; }
tags.add(s.charAt(i));
cnt++;
}
}
if (valid) {
ans = Math.max(ans, cnt);
l = d + 1;
} else {
r = d - 1;
}
}
return ans;
}
}
classSolution {
funmaxPointsInsideSquare(points: Array<IntArray>, s: String): Int {
var l = 0var r = 0var ans = 0for (p in points) r = maxOf(r, kotlin.math.abs(p[0]), kotlin.math.abs(p[1]))
while (l <= r) {
val d = (l + r) / 2val tags = mutableSetOf<Char>()
var cnt = 0var valid = truefor (i in points.indices) {
if (kotlin.math.abs(points[i][0]) <= d && kotlin.math.abs(points[i][1]) <= d) {
if (tags.contains(s[i])) { valid = false; break }
tags.add(s[i])
cnt++ }
}
if (valid) {
ans = maxOf(ans, cnt)
l = d + 1 } else {
r = d - 1 }
}
return ans
}
}
classSolution:
defmaxPointsInsideSquare(self, points: list[list[int]], s: str) -> int:
l, r, ans =0, 0, 0for x, y in points:
r = max(r, abs(x), abs(y))
n = len(points)
while l <= r:
d = (l + r) //2 tags = set()
cnt =0 valid =Truefor i in range(n):
if abs(points[i][0]) <= d and abs(points[i][1]) <= d:
if s[i] in tags:
valid =Falsebreak tags.add(s[i])
cnt +=1if valid:
ans = max(ans, cnt)
l = d +1else:
r = d -1return ans
impl Solution {
pubfnmax_points_inside_square(points: Vec<Vec<i32>>, s: String) -> i32 {
letmut l =0;
letmut r =0;
letmut ans =0;
for p in&points {
r = r.max(p[0].abs()).max(p[1].abs());
}
let n = points.len();
let s = s.as_bytes();
while l <= r {
let d = (l + r) /2;
letmut tags = std::collections::HashSet::new();
letmut cnt =0;
letmut valid =true;
for i in0..n {
if points[i][0].abs() <= d && points[i][1].abs() <= d {
if tags.contains(&s[i]) { valid =false; break; }
tags.insert(s[i]);
cnt +=1;
}
}
if valid {
ans = ans.max(cnt);
l = d +1;
} else {
r = d -1;
}
}
ans
}
}
⏰ Time complexity: O(n log M), where n is the number of points and M is the maximum absolute coordinate value. Each binary search step checks all points.
🧺 Space complexity: O(n), for the set of tags in the worst case.