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

      

  • 相关阅读:
    将smarty安装到MVC架构中
    MVC开发模式以及Smarty模板引擎的使用
    LAMP环境搭建+配置虚拟域名
    第四节 块标签、含样式的标签
    第三节 p标签
    第二节 标题标签
    第一节 简单的html
    第十一节 python和集群交互
    第十节 redis集群搭建
    第九节 搭建主从服务
  • 原文地址:https://www.cnblogs.com/apanda009/p/7121213.html
Copyright © 2011-2022 走看看