@@ -90,32 +90,214 @@ tags:
9090
9191<!-- solution:start -->
9292
93- ### 方法一
93+ ### 方法一:前缀和 + 哈希表
94+
95+ 我们可以使用前缀和数组来记录每一步移动后的位置变化。 具体地,我们使用两个前缀和数组 $f$ 和 $g$ 分别记录每一步移动后在 $x$ 轴和 $y$ 轴上的位置变化。
96+
97+ 初始化 $f[ 0] = 0$ 和 $g[ 0] = 0$,表示初始位置为 $(0, 0)$。然后,我们遍历字符串 $s$,对于每个字符:
98+
99+ - 如果字符为 'U',则 $g[ i] = g[ i-1] + 1$。
100+ - 如果字符为 'D',则 $g[ i] = g[ i-1] - 1$。
101+ - 如果字符为 'L',则 $f[ i] = f[ i-1] - 1$。
102+ - 如果字符为 'R',则 $f[ i] = f[ i-1] + 1$。
103+
104+ 接下来,我们使用一个哈希集合来存储不同的最终坐标。对于每个可能的子字符串移除位置 $i$(从 $k$ 到 $n$),我们计算移除子字符串后的最终坐标 $(a, b)$,其中 $a = f[ n] - (f[ i] - f[ i-k] )$,而 $b = g[ n] - (g[ i] - g[ i-k] )$。将坐标 $(a, b)$ 添加到哈希集合中。
105+
106+ 最后,哈希集合的大小即为不同最终坐标的数量。
107+
108+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
94109
95110<!-- tabs:start -->
96111
97112#### Python3
98113
99114``` python
100-
115+ class Solution :
116+ def distinctPoints (self , s : str , k : int ) -> int :
117+ n = len (s)
118+ f = [0 ] * (n + 1 )
119+ g = [0 ] * (n + 1 )
120+ x = y = 0
121+ for i, c in enumerate (s, 1 ):
122+ if c == " U" :
123+ y += 1
124+ elif c == " D" :
125+ y -= 1
126+ elif c == " L" :
127+ x -= 1
128+ else :
129+ x += 1
130+ f[i] = x
131+ g[i] = y
132+ st = set ()
133+ for i in range (k, n + 1 ):
134+ a = f[n] - (f[i] - f[i - k])
135+ b = g[n] - (g[i] - g[i - k])
136+ st.add((a, b))
137+ return len (st)
101138```
102139
103140#### Java
104141
105142``` java
106-
143+ class Solution {
144+ public int distinctPoints (String s , int k ) {
145+ int n = s. length();
146+ int [] f = new int [n + 1 ];
147+ int [] g = new int [n + 1 ];
148+ int x = 0 , y = 0 ;
149+ for (int i = 1 ; i <= n; ++ i) {
150+ char c = s. charAt(i - 1 );
151+ if (c == ' U' ) {
152+ ++ y;
153+ } else if (c == ' D' ) {
154+ -- y;
155+ } else if (c == ' L' ) {
156+ -- x;
157+ } else {
158+ ++ x;
159+ }
160+ f[i] = x;
161+ g[i] = y;
162+ }
163+ Set<Long > st = new HashSet<> ();
164+ for (int i = k; i <= n; ++ i) {
165+ int a = f[n] - (f[i] - f[i - k]);
166+ int b = g[n] - (g[i] - g[i - k]);
167+ st. add(1L * a * n + b);
168+ }
169+ return st. size();
170+ }
171+ }
107172```
108173
109174#### C++
110175
111176``` cpp
112-
177+ class Solution {
178+ public:
179+ int distinctPoints(string s, int k) {
180+ int n = s.size();
181+ vector<int > f(n + 1), g(n + 1);
182+ int x = 0, y = 0;
183+ for (int i = 1; i <= n; ++i) {
184+ char c = s[ i - 1] ;
185+ if (c == 'U')
186+ ++y;
187+ else if (c == 'D')
188+ --y;
189+ else if (c == 'L')
190+ --x;
191+ else
192+ ++x;
193+ f[ i] = x;
194+ g[ i] = y;
195+ }
196+ unordered_set<long long > st;
197+ for (int i = k; i <= n; ++i) {
198+ int a = f[ n] - (f[ i] - f[ i - k] );
199+ int b = g[ n] - (g[ i] - g[ i - k] );
200+ st.insert(1LL * a * n + b);
201+ }
202+ return st.size();
203+ }
204+ };
113205```
114206
115207#### Go
116208
117209```go
210+ func distinctPoints(s string, k int) int {
211+ n := len(s)
212+ f := make([]int, n+1)
213+ g := make([]int, n+1)
214+ x, y := 0, 0
215+ for i := 1; i <= n; i++ {
216+ c := s[i-1]
217+ if c == 'U' {
218+ y++
219+ } else if c == 'D' {
220+ y--
221+ } else if c == 'L' {
222+ x--
223+ } else {
224+ x++
225+ }
226+ f[i] = x
227+ g[i] = y
228+ }
229+ st := make(map[int64]struct{})
230+ for i := k; i <= n; i++ {
231+ a := f[n] - (f[i] - f[i-k])
232+ b := g[n] - (g[i] - g[i-k])
233+ key := int64(a)*int64(n) + int64(b)
234+ st[key] = struct{}{}
235+ }
236+ return len(st)
237+ }
238+ ```
239+
240+ #### TypeScript
241+
242+ ``` ts
243+ function distinctPoints(s : string , k : number ): number {
244+ const n = s .length ;
245+ const f = new Array (n + 1 ).fill (0 );
246+ const g = new Array (n + 1 ).fill (0 );
247+ let x = 0 ,
248+ y = 0 ;
249+ for (let i = 1 ; i <= n ; ++ i ) {
250+ const c = s [i - 1 ];
251+ if (c === ' U' ) ++ y ;
252+ else if (c === ' D' ) -- y ;
253+ else if (c === ' L' ) -- x ;
254+ else ++ x ;
255+ f [i ] = x ;
256+ g [i ] = y ;
257+ }
258+ const st = new Set <number >();
259+ for (let i = k ; i <= n ; ++ i ) {
260+ const a = f [n ] - (f [i ] - f [i - k ]);
261+ const b = g [n ] - (g [i ] - g [i - k ]);
262+ st .add (a * n + b );
263+ }
264+ return st .size ;
265+ }
266+ ```
118267
268+ #### Rust
269+
270+ ``` rust
271+ use std :: collections :: HashSet ;
272+
273+ impl Solution {
274+ pub fn distinct_points (s : String , k : i32 ) -> i32 {
275+ let n = s . len ();
276+ let mut f = vec! [0 ; n + 1 ];
277+ let mut g = vec! [0 ; n + 1 ];
278+ let mut x = 0 ;
279+ let mut y = 0 ;
280+ let bytes = s . as_bytes ();
281+ for i in 1 ..= n {
282+ match bytes [i - 1 ] as char {
283+ 'U' => y += 1 ,
284+ 'D' => y -= 1 ,
285+ 'L' => x -= 1 ,
286+ _ => x += 1 ,
287+ }
288+ f [i ] = x ;
289+ g [i ] = y ;
290+ }
291+ let mut st = HashSet :: new ();
292+ let k = k as usize ;
293+ for i in k ..= n {
294+ let a = f [n ] - (f [i ] - f [i - k ]);
295+ let b = g [n ] - (g [i ] - g [i - k ]);
296+ st . insert ((a as i64 ) * (n as i64 ) + (b as i64 ));
297+ }
298+ st . len () as i32
299+ }
300+ }
119301```
120302
121303<!-- tabs: end -->
0 commit comments