Skip to content

Commit c357375

Browse files
committed
Time: 237 ms (100%), Space: 79.6 MB (100%) - LeetHub
1 parent e7133f4 commit c357375

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const distinctPoints = (s: string, k: number): number => {
2+
const n = s.length;
3+
const map = new Map<string, number>([
4+
['U', 0],
5+
['D', 0],
6+
['L', 0],
7+
['R', 0]
8+
]);
9+
const set = new Set<string>();
10+
11+
for (let i = 0; i < n; i++) {
12+
// Remove the character that's sliding out of the window
13+
if (i >= k) {
14+
const charToRemove = s[i - k];
15+
map.set(charToRemove, map.get(charToRemove)! - 1);
16+
}
17+
18+
// Add the current character to the window
19+
map.set(s[i], map.get(s[i])! + 1);
20+
21+
// Only process when we have a full window
22+
if (i >= k - 1) {
23+
const key = `${Math.max(0, map.get('U')! - map.get('D')!)}|${Math.max(0, map.get('D')! - map.get('U')!)}|${Math.max(0, map.get('L')! - map.get('R')!)}|${Math.max(0, map.get('R')! - map.get('L')!)}`;
24+
set.add(key);
25+
}
26+
}
27+
return set.size;
28+
};

0 commit comments

Comments
 (0)