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;
    }

      

  • 相关阅读:
    有关选择的心法(2)
    系统故障排查和确认是否被入侵
    伪善还是妥协
    建恒信安日志审计部署
    有关选择的心法
    数据统治世界----37%原则
    控制二分法
    磁盘空间耗尽导致服务器无法启动
    Linux 标准目录结构 FHS
    Linux入侵类问题排查思路
  • 原文地址:https://www.cnblogs.com/apanda009/p/7121213.html
Copyright © 2011-2022 走看看