-
Notifications
You must be signed in to change notification settings - Fork 819
/
LongestWordInDictonary.java
54 lines (50 loc) · 1.86 KB
/
LongestWordInDictonary.java
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
48
49
50
51
52
53
54
package string;
import java.util.*;
/**
* Created by gouthamvidyapradhan on 15/02/2018. Given a string and a string dictionary, find the
* longest string in the dictionary that can be formed by deleting some characters of the given
* string. If there are more than one possible results, return the longest word with the smallest
* lexicographical order. If there is no possible result, return the empty string.
*
* <p>Example 1: Input: s = "abpcplea", d = ["ale","apple","monkey","plea"]
*
* <p>Output: "apple" Example 2: Input: s = "abpcplea", d = ["a","b","c"]
*
* <p>Output: "a" Note: All the strings in the input will only contain lower-case letters. The size
* of the dictionary won't exceed 1,000. The length of all the strings in the input won't exceed
* 1,000.
*
* <p>Solution: O((n x m x log n) + (m ^ 2 + m x n))) sort the dictionary based on the longest first
* and then lexicographically and compare each sorted word with given word and do a two pointer
* comparison to check for sub-sequence.
*/
public class LongestWordInDictonary {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) throws Exception {
List<String> dict = Arrays.asList("ale", "apple", "monkey", "plea");
System.out.println(new LongestWordInDictonary().findLongestWord("abpcplea", dict));
}
public String findLongestWord(String s, List<String> d) {
Collections.sort(
d, Comparator.comparing(String::length).reversed().thenComparing(String::compareTo));
for (String str : d) {
if (str.length() <= s.length()) {
int i = 0, j = 0;
for (int l1 = s.length(), l2 = str.length(); i < l1 && j < l2; ) {
if (s.charAt(i) == str.charAt(j)) {
i++;
j++;
} else {
i++;
}
}
if (j >= str.length()) return str;
}
}
return "";
}
}