3694. Distinct Points Reachable After Substring Removal
Description
You are given a string s consisting of characters 'U', 'D', 'L', and 'R', representing moves on an infinite 2D Cartesian grid.
'U': Move from(x, y)to(x, y + 1).'D': Move from(x, y)to(x, y - 1).'L': Move from(x, y)to(x - 1, y).'R': Move from(x, y)to(x + 1, y).
You are also given a positive integer k.
You must choose and remove exactly one contiguous substring of length k from s. Then, start from coordinate (0, 0) and perform the remaining moves in order.
Return an integer denoting the number of distinct final coordinates reachable.
Example 1:
Input: s = "LUL", k = 1
Output: 2
Explanation:
After removing a substring of length 1, s can be "UL", "LL" or "LU". Following these moves, the final coordinates will be (-1, 1), (-2, 0) and (-1, 1) respectively. There are two distinct points (-1, 1) and (-2, 0) so the answer is 2.
Example 2:
Input: s = "UDLR", k = 4
Output: 1
Explanation:
After removing a substring of length 4, s can only be the empty string. The final coordinates will be (0, 0). There is only one distinct point (0, 0) so the answer is 1.
Example 3:
Input: s = "UU", k = 1
Output: 1
Explanation:
After removing a substring of length 1, s becomes "U", which always ends at (0, 1), so there is only one distinct final coordinate.
Constraints:
1 <= s.length <= 105sconsists of only'U','D','L', and'R'.1 <= k <= s.length
Solutions
Solution 1: Prefix Sum + Hash Table
We can use prefix sum arrays to track position changes after each move. Specifically, we use two prefix sum arrays \(f\) and \(g\) to record the position changes on the \(x\)-axis and \(y\)-axis respectively after each move.
Initialize \(f[0] = 0\) and \(g[0] = 0\), representing the initial position at \((0, 0)\). Then, we iterate through the string \(s\), and for each character:
- If the character is 'U', then \(g[i] = g[i-1] + 1\).
- If the character is 'D', then \(g[i] = g[i-1] - 1\).
- If the character is 'L', then \(f[i] = f[i-1] - 1\).
- If the character is 'R', then \(f[i] = f[i-1] + 1\).
Next, we use a hash set to store the distinct final coordinates. For each possible substring removal position \(i\) (from \(k\) to \(n\)), we calculate the final coordinates \((a, b)\) after removing the substring, where \(a = f[n] - (f[i] - f[i-k])\) and \(b = g[n] - (g[i] - g[i-k])\). Add the coordinates \((a, b)\) to the hash set.
Finally, the size of the hash set is the number of distinct final coordinates.
The time complexity is \(O(n)\) and the space complexity is \(O(n)\), where \(n\) is the length of the string \(s\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |