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.
The main idea is to check for each word in the dictionary if it is a subsequence of the given string. If multiple words are valid, we return the longest one, and if there is a tie, the lexicographically smallest one.
classSolution {
public: string findLongestWord(string s, vector<string>& d) {
string ans ="";
for (auto& w : d) {
int i =0, j =0;
while (i < s.size() && j < w.size()) {
if (s[i] == w[j]) j++;
i++;
}
if (j == w.size()) {
if (w.size() > ans.size() || (w.size() == ans.size() && w < ans)) ans = w;
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
funcfindLongestWord(sstring, d []string) string {
ans:=""for_, w:=ranged {
i, j:=0, 0fori < len(s) &&j < len(w) {
ifs[i] ==w[j] {
j++ }
i++ }
ifj== len(w) {
if len(w) > len(ans) || (len(w) == len(ans) &&w < ans) {
ans = w }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
public String findLongestWord(String s, List<String> d) {
String ans ="";
for (String w : d) {
int i = 0, j = 0;
while (i < s.length() && j < w.length()) {
if (s.charAt(i) == w.charAt(j)) j++;
i++;
}
if (j == w.length()) {
if (w.length() > ans.length() || (w.length() == ans.length() && w.compareTo(ans) < 0)) ans = w;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funfindLongestWord(s: String, d: List<String>): String {
var ans = ""for (w in d) {
var i = 0; var j = 0while (i < s.length && j < w.length) {
if (s[i] == w[j]) j++ i++ }
if (j == w.length) {
if (w.length > ans.length || (w.length == ans.length && w < ans)) ans = w
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
deffindLongestWord(self, s: str, d: list[str]) -> str:
ans =""for w in d:
i = j =0while i < len(s) and j < len(w):
if s[i] == w[j]:
j +=1 i +=1if j == len(w):
if len(w) > len(ans) or (len(w) == len(ans) and w < ans):
ans = w
return ans
impl Solution {
pubfnfind_longest_word(s: String, d: Vec<String>) -> String {
letmut ans = String::new();
for w in d.iter() {
let (mut i, mut j) = (0, 0);
let s_bytes = s.as_bytes();
let w_bytes = w.as_bytes();
while i < s_bytes.len() && j < w_bytes.len() {
if s_bytes[i] == w_bytes[j] { j +=1; }
i +=1;
}
if j == w_bytes.len() {
if w.len() > ans.len() || (w.len() == ans.len() && w <&ans) {
ans = w.clone();
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
findLongestWord(s: string, d: string[]):string {
letans="";
for (constwofd) {
leti=0, j=0;
while (i<s.length&&j<w.length) {
if (s[i] ===w[j]) j++;
i++;
}
if (j===w.length) {
if (w.length>ans.length|| (w.length===ans.length&&w<ans)) ans=w;
}
}
returnans;
}
}
⏰ Time complexity: O(n * m), where n is the number of words in the dictionary and m is the length of the string s, since for each word we may scan all of s.
🧺 Space complexity: O(1), as only a few variables are used for tracking the answer.
By sorting the dictionary by length (descending) and lexicographically (ascending), we can return the first word that is a subsequence of the given string, which guarantees the correct answer.
classSolution {
public String findLongestWord(String s, List<String> d) {
d.sort((a, b) -> b.length() != a.length() ? b.length() - a.length() : a.compareTo(b));
for (String w : d) {
int i = 0, j = 0;
while (i < s.length() && j < w.length()) {
if (s.charAt(i) == w.charAt(j)) j++;
i++;
}
if (j == w.length()) return w;
}
return"";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funfindLongestWord(s: String, d: List<String>): String {
val sorted = d.sortedWith(compareByDescending<String> { it.length }.thenBy { it })
for (w in sorted) {
var i = 0; var j = 0while (i < s.length && j < w.length) {
if (s[i] == w[j]) j++ i++ }
if (j == w.length) return w
}
return"" }
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
deffindLongestWord(self, s: str, d: list[str]) -> str:
d.sort(key=lambda x: (-len(x), x))
for w in d:
i = j =0while i < len(s) and j < len(w):
if s[i] == w[j]:
j +=1 i +=1if j == len(w):
return w
return""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnfind_longest_word(s: String, mut d: Vec<String>) -> String {
d.sort_by(|a, b| b.len().cmp(&a.len()).then(a.cmp(b)));
for w in d.iter() {
let (mut i, mut j) = (0, 0);
let s_bytes = s.as_bytes();
let w_bytes = w.as_bytes();
while i < s_bytes.len() && j < w_bytes.len() {
if s_bytes[i] == w_bytes[j] { j +=1; }
i +=1;
}
if j == w_bytes.len() {
return w.clone();
}
}
String::new()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
findLongestWord(s: string, d: string[]):string {
d.sort((a, b) =>b.length-a.length||a.localeCompare(b));
for (constwofd) {
leti=0, j=0;
while (i<s.length&&j<w.length) {
if (s[i] ===w[j]) j++;
i++;
}
if (j===w.length) returnw;
}
return"";
}
}
⏰ Time complexity: O(n * m + n log n), where n is the number of words in the dictionary and m is the length of the string s, due to sorting and checking subsequences.
🧺 Space complexity: O(n), for sorting the dictionary.