Skip to content

Commit 01d6945

Browse files
authored
Merge pull request #1905 from tanyagupta01/leetcode-524
Create 0524-longest-word-in-dictionary-though-deleting
2 parents 0d63104 + cda079e commit 01d6945

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
id: longest-word-in-dictionary-through-deleting
3+
title: Longest Word in Dictionary through Deleting
4+
sidebar_label: 0524-longest-word-in-dictionary-through-deleting
5+
tags:
6+
- Array
7+
- Two Pointers
8+
- String
9+
- Sorting
10+
- Java
11+
- Python
12+
- C++
13+
description: This is a solution to the longest word in dictionary through deleting problem on LeetCode.
14+
---
15+
16+
## Problem Description
17+
18+
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.
19+
20+
### Examples
21+
22+
**Example 1:**
23+
Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
24+
Output: "apple"
25+
26+
**Example 2**
27+
Input: s = "abpcplea", dictionary = ["a","b","c"]
28+
Output: "a"
29+
30+
### Constraints
31+
- `1 <= s.length <= 1000`
32+
- `1 <= dictionary.length <= 1000`
33+
- `1 <= dictionary[i].length <= 1000`
34+
- `s` and `dictionary[i]` consist of lowercase English letters.
35+
36+
## Solution
37+
38+
### Intuition
39+
The intuition behind the solution is to use a two-pointer approach that can efficiently validate whether a word from the dictionary is a subsequence of the string s. Here's how we proceed:
40+
41+
- Initialize two pointers, i for the index in the string s and j for the index in the current dictionary word.
42+
- Increment i each time we examine a character in s.
43+
- Increment j only when the characters at i in s and j in the dictionary word match.
44+
A dictionary word is a subsequence of s if we can traverse through the entire word (i.e., j equals the length of the word) by selectively matching characters in s.
45+
- By iterating through each word in the dictionary and applying the two-pointer technique, we can check which words can be formed. During the process, we also keep track of the longest word that satisfies the condition. If multiple words have the same length, we choose the one with the lowest lexicographical order, which is the same as saying the smallest in alphabetical order.
46+
47+
The check function implements the two-pointer technique, and the outer loop through the dictionary selects the best candidate word according to the above-mentioned criteria.
48+
49+
#### Code in Different Languages
50+
51+
<Tabs>
52+
<TabItem value="C++" label="C==">
53+
54+
```cpp
55+
//cpp
56+
57+
class Solution {
58+
public:
59+
string findLongestWord(string s, vector<string>& d) {
60+
string ans;
61+
62+
for (const string& word : d)
63+
if (isSubsequence(word, s))
64+
if (word.length() > ans.length() ||
65+
word.length() == ans.length() && word.compare(ans) < 0)
66+
ans = word;
67+
68+
return ans;
69+
}
70+
71+
private:
72+
// Returns true if a is a subsequence of b.
73+
bool isSubsequence(const string& a, const string& b) {
74+
int i = 0;
75+
for (const char c : b)
76+
if (i < a.length() && c == a[i])
77+
++i;
78+
return i == a.length();
79+
};
80+
};
81+
```
82+
</TabItem>
83+
<TabItem value="Java" label="Java">
84+
85+
```java
86+
//java
87+
88+
class Solution {
89+
public String findLongestWord(String s, List<String> d) {
90+
String ans = "";
91+
92+
for (final String word : d)
93+
if (isSubsequence(word, s))
94+
if (word.length() > ans.length() ||
95+
word.length() == ans.length() && word.compareTo(ans) < 0)
96+
ans = word;
97+
98+
return ans;
99+
}
100+
101+
// Returns true if a is a subsequence of b.
102+
private boolean isSubsequence(final String a, final String b) {
103+
int i = 0;
104+
for (final char c : b.toCharArray())
105+
if (i < a.length() && c == a.charAt(i))
106+
++i;
107+
return i == a.length();
108+
}
109+
}
110+
111+
```
112+
</TabItem>
113+
<TabItem value="Python" label="Python">
114+
115+
```python
116+
//python
117+
118+
class Solution:
119+
def findLongestWord(self, s: str, d: List[str]) -> str:
120+
ans = ''
121+
122+
for word in d:
123+
i = 0
124+
for c in s:
125+
if i < len(word) and c == word[i]:
126+
i += 1
127+
if i == len(word):
128+
if len(word) > len(ans) or len(word) == len(ans) and word < ans:
129+
ans = word
130+
131+
return ans
132+
```
133+
134+
</TabItem>
135+
</Tabs>
136+
137+
138+
139+
## References
140+
141+
- **LeetCode Problem:** [Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/)
142+
- **Solution Link:** [Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/solutions/)

0 commit comments

Comments
 (0)