您的位置:首页 > 其它

leetcode 524. Longest Word in Dictionary through Deleting

2017-07-27 14:54 344 查看
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.

Example 1:

Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]

Output:
"apple"


Example 2:

Input:
s = "abpcplea", d = ["a","b","c"]

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.

我就用的我的解法。先把s里的字符和索引放入map中。再对list中的字符串遍历,看看能不能用map中的字符来构成。
package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class Longest_Word_in_Dictionary_through_Deleting_524 {

public String findLongestWord(String s, List<String> d) {
HashMap<Character, ArrayList<Integer>> map=new HashMap<Character, ArrayList<Integer>>();
char[] cs=s.toCharArray();
for(int i=0;i<cs.length;i++){
ArrayList<Integer> list=map.getOrDefault(cs[i], new ArrayList<Integer>());
list.add(i);
map.put(cs[i], list);
}
int maxLength=0;
ArrayList<String> candidates=new ArrayList<String>();
for(String string:d){
int end=-1;
boolean isOK=true;
for(int i=0;i<string.length();i++){
char c=string.charAt(i);
if(map.get(c)==null){
isOK=false;
break;
}
ArrayList<Integer> indexes=map.get(c);
boolean isFind=false;
for(Integer index:indexes){
if(index>end){
isFind=true;
end=index;
break;
}
}
if(isFind==false){
isOK=false;
break;
}
}
if(isOK==true){
if(string.length()>maxLength){
maxLength=string.length();
candidates.clear();
candidates.add(string);
}
else if(string.length()==maxLength){
candidates.add(string);
}
}
}
if(candidates.size()==0){
return "";
}
Collections.sort(candidates);
return candidates.get(0);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Longest_Word_in_Dictionary_through_Deleting_524 l=new Longest_Word_in_Dictionary_through_Deleting_524();
List<String> d=new ArrayList<String>();
d.add("a");d.add("b");d.add("c");
System.out.println(l.findLongestWord("abpcplea", d));
}

}


大神的解法是:先将list中的元素按照 first by length DESC then lexicographical ASC 来排序,之后再遍历list,看list中的元素 p 是否是 s 的子序列。第一个符合的,就是我们要的结果。
public String findLongestWord(String s, List<String> d) {
if (s.length() == 0 || d.size() == 0) return "";

Collections.sort(d, new Comparator < String > () {
public int compare(String s1, String s2) {
return s2.length() != s1.length() ? s2.length() - s1.length() : s1.compareTo(s2);
}
});

for (String p : d) {
if (s.length() < p.length()) continue;
if (isSubSeq(s, p)) return p;
}

return "";
}

private boolean isSubSeq(String s, String p) {
int i = 0, j = 0;
while (i < s.length() && j < p.length()) {
if (s.charAt(i) == p.charAt(j)) {
i++; j++;
}
else {
i++;
}
}
return j == p.length();
}


大神还有不需要使用sort的方法:
因为sorting the dictionary 会带来较大的开销,我们可以跳过sorting,直接来看未排序的dictionary中的元素string xx 是否是 s 的子序列。当这样的一个 xx 符合要求时,我们将xx与其他符合要求的字符串比较 大小和字典序。如此,需要遍历 d 中的所有字符串。

public String findLongestWord(String s, List < String > d) {
String max_str = "";
for (String str: d) {
if (isSubSeq(s, str)) {
if (str.length() > max_str.length() || (str.length() == max_str.length() && str.compareTo(max_str) < 0))
max_str = str;
}
}
return max_str;
}

private boolean isSubSeq(String s, String p) {
int i = 0, j = 0;
while (i < s.length() && j < p.length()) {
if (s.charAt(i) == p.charAt(j)) {
i++; j++;
}
else {
i++;
}
}
return j == p.length();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: