美文网首页
Longest Word in Dictionary throu

Longest Word in Dictionary throu

作者: Frank_Kivi | 来源:发表于2018-06-28 10:06 被阅读6次

    https://www.lintcode.com/problem/longest-word-in-dictionary-through-deleting/description

    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Solution {
        /**
         * @param s: a string
         * @param d: List[str]
         * @return: return a string
         */
        public String findLongestWord(String s, List<String> d) {
            // write your code  here
            Collections.sort(d, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    if (o2.length() == o1.length()) {
                        return o1.compareTo(o2);
                    }
                    return o2.length() - o1.length();
                }
            });
            test:
            for (int i = 0; i < d.size(); i++) {
                String s1 = d.get(i);
                int index = 0;
                for (char c : s1.toCharArray()) {
                    index = s.indexOf(c, index);
                    if (index < 0) {
                        continue test;
                    }
                    index++;
                }
                return s1;
            }
            return "";
        }
    }
    

    相关文章

      网友评论

          本文标题:Longest Word in Dictionary throu

          本文链接:https://www.haomeiwen.com/subject/ywzwyftx.html