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 = "";   // 用来比较的local
    for (String dictWord : d) {  // 题意: 顺序遍历
        int i = 0;               // 每次都得从target头部开始遍历
        for (char c : s.toCharArray())   // 判断字符串的每一个字母相等用toCharArray
            if (i < dictWord.length() && c == dictWord.charAt(i)) i++; 题意: 比较

        if (i == dictWord.length() && dictWord.length() >= longest.length()) //判断是否满足题意
            if (dictWord.length() > longest.length() || dictWord.compareTo(longest) < 0) // 是否满足两点, 更新global,                                           学会s.compareTo(ss) 来比较字典顺序
         longest = dictWord;  }   return longest; } 

Time Complexity: O(nk), where n is the length of string s and k is the number of words in the dictionary.

  

时间: 2024-08-26 17:30:28

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

[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 lexicographic

[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] 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

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

[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.那么我们首先来想如果是拆分两个单词怎么做,那我们要首先把所有的单词存到哈希表里,然后遍历每个单词,每个位置上都拆分成左右两个字符

富文本编辑器内容生成Word 与 Dictionary的使用

@{ ViewBag.Title = "知识上传";}@model Dictionary<string, bool> //表单验证必填项目不为空函数 $(function () { if ("@Model["upload"]" == "True") { $('body').dailog({ type: 'primary', title: '提示.', discription: '上传成功!' }); } else

Find the Longest Word in a String

找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. function findLongestWord(str) { var array=str.split(" "); var newArray=[]; for(var i=0;i<array.length;i++){ newArray.push(array[i].length); } function word (a,b){ return b-a; } var last = newArray.sort(wo