Given a string s and a character c that occurs in s, return an array of integers answer where answer.length == s.length and answer[i] is the distance from index i to the closest occurrence of character c in s.
The distance between two indices i and j is abs(i - j), where abs is the absolute value function.
Example 1:
Input: s = "loveleetcode", c = "e" Output: [3,2,1,0,1,0,0,1,2,2,1,0] Explanation: The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed). The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3. The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2. For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5, but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1. The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.
Example 2:
Input: s = "aaab", c = "b" Output: [3,2,1,0]
Constraints:
1 <= s.length <= 104s[i] and c are lowercase English letters.c occurs at least once in s.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find the shortest distance from each letter in a word to a specific target letter. The brute force method involves checking the distance from every letter in the word to every instance of the target letter, one at a time, to find the closest one.
Here's how the algorithm would work step-by-step:
def shortest_distance_to_character_brute_force(word, target_character):
shortest_distances = []
for current_index in range(len(word)):
# Find the shortest distance for current character
minimum_distance = float('inf')
for target_index in range(len(word)):
# Check every target to find the minimum distance
if word[target_index] == target_character:
distance = abs(current_index - target_index)
minimum_distance = min(minimum_distance, distance)
shortest_distances.append(minimum_distance)
return shortest_distancesThe problem asks us to find, for each letter in a word, the shortest distance to a specific target character. We can solve this efficiently by making two passes through the word, one from left to right and one from right to left, keeping track of the most recent location of our target character.
Here's how the algorithm would work step-by-step:
def shortest_distance_to_character(word, target_character):
length_of_word = len(word)
shortest_distances = [float('inf')] * length_of_word
# First pass: left to right.
closest_target_position = float('-inf')
for index in range(length_of_word):
if word[index] == target_character:
# Reset the closest target when found.
closest_target_position = index
if closest_target_position != float('-inf'):
shortest_distances[index] = index - closest_target_position
# Second pass: right to left.
closest_target_position = float('inf')
for index in range(length_of_word - 1, -1, -1):
if word[index] == target_character:
# Reset the closest target when found.
closest_target_position = index
if closest_target_position != float('inf'):
distance = abs(index - closest_target_position)
# Choose minimum distance from both sides.
shortest_distances[index] = min(shortest_distances[index], distance)
return shortest_distances| Case | How to Handle |
|---|---|
| Empty string S | Return an empty list immediately, as there are no characters to compute distances for. |
| Null string S or null character C | Throw an IllegalArgumentException or return null, depending on the language conventions, to indicate invalid input. |
| String S has length 1 | If the single character is equal to C, the distance is 0; otherwise, the distance is 1. |
| Character C does not exist in string S | Return a list where all distances are equal to the length of the string, or use a large constant representing infinity. |
| Character C appears at the beginning and/or end of string S | The algorithm should correctly compute distances from these boundary occurrences of C. |
| String S contains only the character C | Return a list of all zeros, as every character is the target character. |
| Very long string S, approaching maximum string length supported by the language | Ensure the solution's space complexity is efficient, avoiding excessive memory allocation that could lead to out-of-memory errors; a two-pass approach minimizes storage impact. |
| Multiple occurrences of character C are clustered together | The algorithm must correctly handle overlapping distance ranges from adjacent occurrences of C. |