zoukankan      html  css  js  c++  java
  • 524. Longest Word in Dictionary through Deleting

    https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/solutions

    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.
    Time Complexity: O(nk), where n is the length of string s and k is the number of words in the dictionary.
    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;
    }

      

  • 相关阅读:
    自动滑块验证登录QQ-java实现
    今日校园自动提交问卷-Java实现
    文库下载实现自动化
    测试
    软件工程结课小结
    结对项目-java生成四则运算
    JS判断对象为空的三种方法
    vue 组件间 8 大通讯方式 之三 eventBus
    vue 组件间 8 大通讯方式 之二 provide/ inject ref / refs
    vue 组件间 8 大通讯方式 之一 props / $emit $children / $parent
  • 原文地址:https://www.cnblogs.com/apanda009/p/7121213.html
Copyright © 2011-2022 走看看