Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"] Output: "apple"
Example 2:
Input: s = "abpcplea", dictionary = ["a","b","c"] Output: "a"
Constraints:
1 <= s.length <= 10001 <= dictionary.length <= 10001 <= dictionary[i].length <= 1000s and dictionary[i] consist of 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 approach to finding the longest hidden word involves checking every single candidate word from the given list. For each candidate, we determine if it can be formed by crossing out zero or more letters from the main string, and finally select the best match based on length and alphabetical order.
Here's how the algorithm would work step-by-step:
def find_longest_word(main_string, candidate_words):
winning_match = ""
# Iterate through each candidate to see if it can be formed by deleting letters from the main string
for candidate_word in candidate_words:
main_index = 0
candidate_index = 0
while main_index < len(main_string) and candidate_index < len(candidate_word):
if main_string[main_index] == candidate_word[candidate_index]:
candidate_index += 1
main_index += 1
# Check if the entire candidate word was successfully matched in order
if candidate_index == len(candidate_word):
if len(candidate_word) > len(winning_match):
winning_match = candidate_word
elif len(candidate_word) == len(winning_match) and candidate_word < winning_match:
winning_match = candidate_word
# Return the best match found according to length and lexicographical priority
return winning_matchThe optimal strategy is to evaluate each word from a given collection to see if it can be formed by carefully removing certain letters from a base string, while keeping the remaining letters in their original order. By systematically comparing every candidate word against our base string and keeping track of the best match found so far based on length and alphabetical order, we can efficiently find the correct answer.
Here's how the algorithm would work step-by-step:
def find_longest_word(base_string, dictionary_words):
longest_valid_word = ""
# We sort the dictionary to ensure alphabetical precedence for words of equal length.
sorted_dictionary = sorted(dictionary_words)
for candidate_word in sorted_dictionary:
base_string_index = 0
candidate_index = 0
while base_string_index < len(base_string) and candidate_index < len(candidate_word):
if base_string[base_string_index] == candidate_word[candidate_index]:
candidate_index += 1
base_string_index += 1
# A word is valid only if all its characters were matched sequentially.
if candidate_index == len(candidate_word):
# We only update if the new word strictly exceeds the current length due to sorting.
if len(candidate_word) > len(longest_valid_word):
longest_valid_word = candidate_word
return longest_valid_word| Case | How to Handle |
|---|---|
| Empty string s or empty dictionary array | Return an empty string immediately since no valid word can ever be formed. |
| Dictionary contains strings longer than the source string s | The subsequence check naturally returns false when a dictionary word exceeds the length of s. |
| Multiple words in the dictionary are valid and have the exact same maximum length | Pre-sorting the dictionary lexicographically or tracking the best match alphabetically ensures we return the smallest lexicographical result. |
| No words in the dictionary can be formed by deleting characters from s | The result variable remains empty, and the function correctly returns an empty string. |
| Dictionary contains duplicate strings | Duplicate words are processed identically and safely without affecting correctness, though sorting handles them adjacent to each other. |
| Source string s and dictionary words consist of special characters, numbers, or mixed casing | Standard character-by-character comparison handles any valid Unicode characters as long as exact equality is checked. |
| Extremely large dictionary with very long strings | Iterating through the dictionary and performing linear-time subsequence checks ensures memory efficiency without recursion stack overflow. |