You are given a 0-indexed string s
, a string a
, a string b
, and an integer k
.
An index i
is beautiful if:
0 <= i <= s.length - a.length
s[i..(i + a.length - 1)] == a
j
such that:
0 <= j <= s.length - b.length
s[j..(j + b.length - 1)] == b
|j - i| <= k
Return the array that contains beautiful indices in sorted order from smallest to largest.
Example 1:
Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 Output: [16,33] Explanation: There are 2 beautiful indices: [16,33]. - The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15. - The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15. Thus we return [16,33] as the result.
Example 2:
Input: s = "abcd", a = "a", b = "a", k = 4 Output: [0] Explanation: There is 1 beautiful index: [0]. - The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4. Thus we return [0] as the result.
Constraints:
1 <= k <= s.length <= 105
1 <= a.length, b.length <= 10
s
, a
, and b
contain only lowercase English letters.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:
The brute force method for this problem involves examining every possible position to see if it meets the criteria. We will check each spot to see if it can be part of the final answer. Because this is brute force, we won't be clever, just thorough.
Here's how the algorithm would work step-by-step:
def find_beautiful_indices_brute_force(main_string, substring_one, substring_two, maximum_difference):
beautiful_indices = []
for first_string_index in range(len(main_string)):
# Consider each index as a potential beautiful index
is_beautiful = False
for second_string_index in range(len(main_string)):
# Search for matches to the second string
if main_string[second_string_index:second_string_index + len(substring_two)] == substring_two:
# Check the distance between indices
if abs(first_string_index - second_string_index) <= maximum_difference:
# Mark as beautiful and stop checking
is_beautiful = True
break
if is_beautiful and main_string[first_string_index:first_string_index + len(substring_one)] == substring_one:
# Add the index to result if its beautiful
beautiful_indices.append(first_string_index)
return beautiful_indices
The efficient solution identifies matches by pre-processing the text. It then uses this information to quickly check the distance between matches instead of re-searching every time.
Here's how the algorithm would work step-by-step:
def find_beautiful_indices(text, word1, word2, allowed_distance):
first_word_indices = []
second_word_indices = []
beautiful_indices = []
# Find all starting indices of word1 in text.
for index in range(len(text) - len(word1) + 1):
if text[index:index + len(word1)] == word1:
first_word_indices.append(index)
# Find all starting indices of word2 in text.
for index in range(len(text) - len(word2) + 1):
if text[index:index + len(word2)] == word2:
second_word_indices.append(index)
# Iterate through the indices of the first word.
for first_word_index in first_word_indices:
# Check if any index of the second word is within the allowed distance.
for second_word_index in second_word_indices:
if abs(first_word_index - second_word_index) <= allowed_distance:
# Add the index to the result if it's beautiful.
beautiful_indices.append(first_word_index)
break # Optimization: Once found, no need to check others.
return sorted(beautiful_indices)
Case | How to Handle |
---|---|
s is null or empty | Return an empty list if s is null or empty as no indices can be found. |
a is null or empty | Return an empty list if a is null or empty as no indices for 'a' can be found. |
b is null or empty | Return an empty list if b is null or empty as no indices for 'b' can be found. |
k is zero | The algorithm must find indices i and j that are exactly the same; iterate through a_indices and check if it exists in b_indices. |
a or b is longer than s | Return an empty list because the substrings 'a' or 'b' cannot be found in s. |
s, a, and b are identical and k is small | Ensure all indices are marked as beautiful since |i-j| <= k always holds for close indices. |
Large input string s | Ensure the solution has reasonable time complexity (e.g., O(n*m) where n is the length of s and m is the length of a and b) by using string searching algorithms efficiently. |
No beautiful indices exist | The solution should return an empty sorted list when no indices i and j satisfy the condition |i - j| <= k. |