VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-largest-word-dictionary-deleting-characters-given-string/

⇱ Longest Matching in dictionary by deleting some characters of given string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest Matching in dictionary by deleting some characters of given string

Last Updated : 2 Jun, 2026

Given a lowercase string s and a dictionary d[] containing lowercase words, find the longest word in the dictionary that can be obtained by deleting some characters from s without changing the order of the remaining characters. If multiple words have the same maximum length, return the lexicographically smallest one. If no valid word exists, return an empty string.

Examples: 

Input: d = ["ale", "apple", "monkey", "plea"], s = "abpcplea"
Output: "apple" 
Explanation: After deleting "b", "c", "a" s became "apple" which is present in d.

Input: d = ["a", "b", "c"], s = "abpcplea"
Output: "a"
Explanation: After deleting "b", "p", "c", "p", "l", "e", "a" s became "a" which is present in d.

[Naive Approach] Check Every Dictionary Word as a Subsequence - O(n * |s|) Time O(1) Space

The idea is to traverse every word in the dictionary and check whether it is a subsequence of the given string s using two pointers. If a word is a valid subsequence, compare it with the current answer. Update the answer if the word has a greater length, or if lengths are equal and the word is lexicographically smaller.


Output
apple

Time Complexity: O(n * |s|)
Auxiliary Space: O(1)

[Expected Approach] Index Mapping + Binary Search - O(|s| + n * maxWordLen * log |s|) Time O(|s|) Space

The idea is to first store all positions of every character present in s in an array of arrays. Now for each dictionary word, try to match its characters in order. Using binary search, find the next occurrence of each character after the previously matched position. If all characters of a word can be matched, then it is a valid subsequence. Among all valid words, choose the longest one, and if multiple words have the same length, choose the lexicographically smallest.

Let us understand with example:
Input: d = ["ale", "apple", "monkey", "plea"], s = "abpcplea"

  • For s = "abpcplea", store the indices of each character in separate lists. Initially, res = "".
  • Check "ale" and use binary search to find 'a' at index 0, 'l' at index 5, and 'e' at index 6. Hence, "ale" is a valid subsequence and res = "ale".
  • Next, for "apple", find 'a' -> 0, 'p' -> 2, 'p' -> 4, 'l' -> 5, and 'e' -> 6. It is also a valid subsequence and is longer than "ale", so update res = "apple".
  • The word "monkey" is not a subsequence since 'm' does not occur in s, while "plea" is shorter than the current result "apple", so it is skipped. Therefore, the final answer is "apple".

Output
apple

Time Complexity: O(|s| + n * maxWordLen * log |s|)
Auxiliary Space: O(|s|)

Comment
Article Tags: