Skip to content

Commit 58ae069

Browse files
authored
Added Longest Word in Dictionary through Deleting.java
1 parent f760460 commit 58ae069

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public String findLongestWord(String s, List<String> d) {
3+
d.sort((o1, o2) -> o1.length() != o2.length() ? Integer.compare(o2.length(), o1.length())
4+
: o1.compareTo(o2));
5+
for (String word : d) {
6+
int wordIdx = 0;
7+
int strIdx = 0;
8+
while (strIdx < s.length() && wordIdx < word.length()) {
9+
if (word.charAt(wordIdx) == s.charAt(strIdx)) {
10+
wordIdx++;
11+
}
12+
strIdx++;
13+
}
14+
if (wordIdx == word.length()) {
15+
return word;
16+
}
17+
}
18+
return "";
19+
}
20+
}

0 commit comments

Comments
 (0)