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

      

  • 相关阅读:
    $.cookie 使用不了的问题定位过程
    jquery.cookie.js使用介绍
    java 转换 小函数(不断增加中。。。)
    jquery ajax 访问webServer的xml文件
    JS中的prototype【转】
    【转载】习惯决定性格 性格决定命运
    jquery的ajax和原始的ajax这两种方式的使用方法
    ajax readyState的五种状态详解
    一个简单的tcp代理实现
    go tcp使用
  • 原文地址:https://www.cnblogs.com/apanda009/p/7121213.html
Copyright © 2011-2022 走看看