KatInterview

524. Longest Word in Dictionary through Deleting

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find the longest string from a given dictionary that can be formed by deleting zero or more characters from a string s. This means we are looking for a word in the dictionary that is a subsequence of s. If multiple words have the same maximum length, we should choose the one that is lexicographically smallest. If no such word exists, an empty string should be returned.

Solution

Longest Word in Dictionary through Deleting in Python

class Solution:
    def findLongestWord(self, s: str, dictionary: List[str]) -> str:
        longest_word = ""  # Stores the best result found so far
        
        for word in dictionary:
            # Check if 'word' is a subsequence of 's' using two pointers
            i = 0  # pointer for s
            j = 0  # pointer for word
            
            while i < len(s) and j < len(word):
                if s[i] == word[j]:
                    j += 1  # Match found, advance pointer for word
                i += 1  # Always advance pointer for s
            
            # If j reaches the end of word, it's a subsequence
            if j == len(word):
                # Check if the current word is a better answer
                # Condition 1: It's longer than the current longest word
                # Condition 2: Same length, but lexicographically smaller
                if len(word) > len(longest_word) or \
                   (len(word) == len(longest_word) and word < longest_word):
                    longest_word = word
                    
        return longest_word

Longest Word in Dictionary through Deleting in Java

class Solution {
    public String findLongestWord(String s, List<String> dictionary) {
        String longestWord = ""; // Stores the best result found so far

        for (String word : dictionary) {
            // Check if 'word' is a subsequence of 's' using two pointers
            int i = 0; // pointer for s
            int j = 0; // pointer for word

            while (i < s.length() && j < word.length()) {
                if (s.charAt(i) == word.charAt(j)) {
                    j++; // Match found, advance pointer for word
                }
                i++; // Always advance pointer for s
            }

            // If j reaches the end of word, it's a subsequence
            if (j == word.length()) {
                // Check if the current word is a better answer
                // Condition 1: It's longer than the current longest word
                // Condition 2: Same length, but lexicographically smaller
                if (word.length() > longestWord.length() || 
                   (word.length() == longestWord.length() && word.compareTo(longestWord) < 0)) {
                    longestWord = word;
                }
            }
        }

        return longestWord;
    }
}

Longest Word in Dictionary through Deleting in C++

class Solution {
public:
    string findLongestWord(string s, vector<string>& dictionary) {
        string longestWord = ""; // Stores the best result found so far
        
        for (const string& word : dictionary) {
            // Check if 'word' is a subsequence of 's' using two pointers
            int i = 0; // pointer for s
            int j = 0; // pointer for word
            
            while (i < s.length() && j < word.length()) {
                if (s[i] == word[j]) {
                    j++; // Match found, advance pointer for word
                }
                i++; // Always advance pointer for s
            }
            
            // If j reaches the end of word, it's a subsequence
            if (j == word.length()) {
                // Check if the current word is a better answer
                // Condition 1: It's longer than the current longest word
                // Condition 2: Same length, but lexicographically smaller
                if (word.length() > longestWord.length() || 
                   (word.length() == longestWord.length() && word < longestWord)) {
                    longestWord = word;
                }
            }
        }
        
        return longestWord;
    }
};

Longest Word in Dictionary through Deleting in TypeScript

function findLongestWord(s: string, dictionary: string[]): string {
    let longestWord = ""; // Stores the best result found so far

    for (const word of dictionary) {
        // Check if 'word' is a subsequence of 's' using two pointers
        let i = 0; // pointer for s
        let j = 0; // pointer for word

        while (i < s.length && j < word.length) {
            if (s[i] === word[j]) {
                j++; // Match found, advance pointer for word
            }
            i++; // Always advance pointer for s
        }

        // If j reaches the end of word, it's a subsequence
        if (j === word.length) {
            // Check if the current word is a better answer
            // Condition 1: It's longer than the current longest word
            // Condition 2: Same length, but lexicographically smaller
            if (word.length > longestWord.length ||
               (word.length === longestWord.length && word < longestWord)) {
                longestWord = word;
            }
        }
    }

    return longestWord;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.