Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Longest word in dictionary through deleting problem solution

YASH PAL, 31 July 2024

In this Leetcode Longest word in a dictionary through deleting problem solution we have 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.

Leetcode Longest word in dictionary through deleting problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

def findLongestWord(self, S, D):
    D.sort(key = lambda x: (-len(x), x))
    for word in D:
        i = 0
        for c in S:
            if i < len(word) and word[i] == c:
                i += 1
        if i == len(word):
            return word
    return ""

Problem solution in Java.

class Solution {
	public String findLongestWord(String s, List<String> dictionary) {

		String answer = "";
		String temAnwer = "";
		Collections.sort(dictionary);

		for (String vocabulary : dictionary) {
			int pointer = 0;
			int vocabularyLength = vocabulary.length();
			vocabulary.charAt(pointer);

			for (int i = 0; i < s.length(); i++) {

				if (vocabulary.charAt(pointer) == s.charAt(i)) {

					pointer++;

				}

				if (pointer == vocabularyLength) {
					temAnwer = vocabulary;
					break;
				}

			}

			if (temAnwer.length() > answer.length()) {
				answer = temAnwer;
			}

		}

		return answer;
	}
}

Problem solution in C++.

class Solution {
public:
    
    bool isSubsequence(string a, string b) {
        // to check if string b is a subsequence of a
        int ai = 0;
        int bi = 0;
        while(ai < a.length() and bi < b.length()) {
            if(a[ai]==b[bi]) {
                ai++;
                bi++;
            } else {
                ai++;
            }
        }
        if(bi==b.length()) {
            return true;
        } 
        return false;
    }
    
    string findLongestWord(string s, vector<string>& dictionary) {
        string ans = "";
        for(string b: dictionary) {
            if(isSubsequence(s,b)) {
                if(b.size() > ans.size() or (b.size()==ans.size() and b < ans)) {
                    ans = b;
                }
            }
        }
        return ans;
    }
};

Problem solution in C.

#include <string.h>

static int cmp(void *a, void *b) {
	int res = strlen(*(char**)b) - strlen(*(char**)a);
	return res == 0 ? strcmp(*(char**)a, *(char**)b) : res;
}

static int findOneWord(char *s, char *d) {
	for (; *s && *d; s++)
		if (*s ==  *d)
			d++;
	return !*d;
}

char* findLongestWord(char* s, char** d, int dSize) {
	qsort(d, dSize, sizeof(char**), cmp);
	for (; *d; d++)
		if (findOneWord(s, *d))
			return *d;
	return ""; 
}

coding problems solutions Leetcode Problems Solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes