-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestWordInDictionaryThroughDeleting.java
More file actions
47 lines (40 loc) · 1.59 KB
/
LongestWordInDictionaryThroughDeleting.java
File metadata and controls
47 lines (40 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package solutions;
import java.util.List;
// [Problem] https://leetcode.com/problems/longest-word-in-dictionary-through-deleting
class LongestWordInDictionaryThroughDeleting {
// Two pointers
// O(n * l) time, O(1) space
// where n = number of words in dictionary, l = string length
public String findLongestWord(String s, List<String> dictionary) {
String longestWord = "";
for (String word : dictionary) {
if (isValid(s, word) && isLonger(word, longestWord)) {
longestWord = word;
}
}
return longestWord;
}
private boolean isValid(String s, String word) {
int wordIndex = 0;
for (int i = 0; i < s.length(); i++) {
if (wordIndex == word.length()) {
return true;
} else if (s.charAt(i) == word.charAt(wordIndex)) {
wordIndex++;
}
}
return wordIndex == word.length();
}
private boolean isLonger(String word1, String word2) {
return word1.length() > word2.length() || (word1.length() == word2.length() && word1.compareTo(word2) < 0);
}
// Test
public static void main(String[] args) {
LongestWordInDictionaryThroughDeleting solution = new LongestWordInDictionaryThroughDeleting();
String s = "abpcplea";
List<String> dictionary = List.of("ale", "apple", "monkey", "plea");
String expectedOutput = "apple";
String actualOutput = solution.findLongestWord(s, dictionary);
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
}
}