[LeetCode] 524. Longest Word in Dictionary through Deleting

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:

  1. All the strings in the input will only contain lower-case letters.
  2. The size of the dictionary won‘t exceed 1,000.
  3. The length of all the strings in the input won‘t exceed 1,000.

题意:找出d中的最长的s的子串

从d中一个一个判断就行了。。(其实可以维护一个长度标记,小于这个长度就不判断了,可以优化一些。由于当时写的着急,这里就不写了)

class Solution {
    public String findLongestWord(String s, List<String> d) {
        TreeSet<String> set = new TreeSet<String>((s1, s2)-> {
            if (s1.length() < s2.length())
                return 1;
            if (s1.length() > s2.length())
                return -1;
            if (s1.compareTo(s2) > 0)
                return 1;
            if (s1.compareTo(s2) < 0)
                return -1;
            return 0;
        });
        for (String str : d) {
            if (isVal(s, str))
                set.add(str);
        }
        if (set.size() == 0)
            return "";
        return set.first();
    }
    public boolean isVal(String superStr, String subStr) {
        int i = 0;
        int j = 0;
        while (i < superStr.length()) {
            if (superStr.charAt(i) == subStr.charAt(j)) {
                i ++;
                j ++;
                if (j == subStr.length())
                    return true;
            }
            else
                i ++;
        }
        return false;
    }
}

原文地址:https://www.cnblogs.com/Moriarty-cx/p/9823296.html

时间: 2024-10-02 18:52:14

[LeetCode] 524. Longest Word in Dictionary through Deleting的相关文章

524. Longest Word in Dictionary through Deleting

https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/solutions An alternate, more efficient solution which avoids sorting the dictionary: public String findLongestWord(String s, List<String> d) { String longest = "";

[LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

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 lexicographic

leetcode 720. Longest Word in Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

[leetcode]720. Longest Word in Dictionary字典中最长的单词

b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心虽然是hashtable,但是这个方法还是很重要的,因为自己实现比较字符串很麻烦 /* 用一个Hashset来判断子串是不是在集合里 然后遍历出最长的就行 长度相同的用compareTo方法判断谁的字典顺序靠前 这个题最难的感觉其实是不知道compareTo方法,自己实现还挺麻烦的 */ publi

[LeetCode] Longest Word In Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

Hash Table-720. Longest Word in Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

[CareerCup] 18.7 Longest Word 最长的单词

5.7 Given a list of words, write a program to find the longest word made of other words in the list. 这道题给了我们一个字符串数组,让我们找到最长的那个单词是由字符串数组中的其他单词组成的,LeetCode上跟类似的题目有Word Break和Word Break II.那么我们首先来想如果是拆分两个单词怎么做,那我们要首先把所有的单词存到哈希表里,然后遍历每个单词,每个位置上都拆分成左右两个字符

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个