Similar Problems
Similar Problems not available
Shortest Distance To Target String In A Circular Array - Leetcode Solution
Companies:
LeetCode: Shortest Distance To Target String In A Circular Array Leetcode Solution
Difficulty: Easy
Problem Statement:
You have a circular array nums of length n. From any index i of nums, you can jump to any index:
i + 1 modulo n where 0 <= i < n. i - 1 modulo n where 0 <= i < n. After each jump, you can choose to continue jumping from the previous position or to start jumping from somewhere else in the array.
You are trying to find a string target of length m that contains only digits. It is guaranteed that target could be formed in the circular array nums and starting from some index. If the target string can be formed multiple times in the circular array, return the minimum start index of such a substring. Otherwise, return -1.
Example 1: Input: nums = [1,2,4,5,2,3,1,4], target = "231" Output: 2 Explanation: The target string could be found in nums starting from index 2:
- Jump from index 2 -> 3 -> 4 -> 0 to obtain the target string "231". Thus index 2 is the minimum start index of such a substring.
Example 2: Input: nums = [1,2,4,5,2,3,1,4], target = "123" Output: -1 Explanation: There is no substring containing all digits of the target string "123" in the circular array nums.
Solution:
To solve this problem, we will use a sliding window approach. We will maintain two pointers called left and right, representing the start and end indices of the window. We will keep track of the frequency of digits in the window using an array count.
Initially, we will set left and right pointers to index 0 and initialize count array with all 0s.
We will then iterate through the circular array nums and for each index i, we will increment the count[nums[i]] by 1.
If the length of the window (right - left + 1) is less than the length of the target string m, we will continue iterating.
Otherwise, if the frequency count of each digit in the window matches the frequency count of each digit in the target string, we have found a substring. We will then check if this substring is shorter than the previously found substring and update the start index accordingly.
If the substring is not found, we will remove the digits from the left end of the window by decrementing count[nums[left]] by 1 and moving the left pointer to the next index. We will continue doing this until a substring is found or we have finished iterating through the array.
Note that since we are using a circular array, we need to consider all possible starting indices of the array. We will simply loop through all possible start indices and perform the above procedure for each start index.
The time complexity of this approach is O(nm), where n is the length of the circular array nums and m is the length of the target string. This is because for each start index, we need to iterate through at most n elements and check at most m digits in the window.
Here's the Python code for the same:
def shortestDistance(self, nums: List[int], target: str) -> int: n = len(nums) m = len(target) best = float('inf') # to store the minimum start index of substring
for i in range(n): # loop through all possible start indices
left = i
right = i
count = [0]*10 # to keep track of the frequency of digits in the window
while right - left + 1 <= m+n: # keep iterating until we have considered all elements
if right - left + 1 > m: # if window size is bigger than target string, remove elements from left end
count[nums[left]] -= 1
left += 1
count[nums[right]] += 1 # add elements to the right end of window
if right - left + 1 >= m and all(count[int(d)] >= target.count(d) for d in target): # check if substring found
if right >= left:
best = min(best, left)
else:
best = min(best, left, right+n) # to handle circular array
break
right = (right+1) % n # move the end pointer to the next index
return -1 if best == float('inf') else best
Shortest Distance To Target String In A Circular Array Solution Code
1