字典中最长的一个单词,可以通过给定单词通过删除某些字符得到

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 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.

解决:

① 这道题给了我们一个字符串,和一个字典,让我们找到字典中最长的一个单词,这个单词可以通过给定单词通过删除某些字符得到。由于只能删除某些字符,并不能重新排序,所以我们不能通过统计字符出现个数的方法来判断是否能得到该单词,而是只能老老实实的按顺序遍历每一个字符。

我们可以给字典排序,通过重写comparator来实现按长度由大到小来排,如果长度相等的就按字母顺序来排。然后我们开始遍历每一个单词,用一个变量i来记录单词中的某个字母的位置,我们遍历给定字符串,如果遍历到单词中的某个字母来,i自增1,如果没有,就继续往下遍历。这样如果最后i和单词长度相等,说明单词中的所有字母都按顺序出现在了字符串s中,由于字典中的单词已经按要求排过序了,所以第一个通过验证的单词一定是正确答案,我们直接返回当前单词即可。

class Solution {//32ms
    public String findLongestWord(String s, List<String> d) {
        if (d == null || d.size() == 0) return "";
        String[] dirs = new String[d.size()];
        for(int i = 0;i < d.size();i ++){
            dirs[i] = d.get(i);
        }
        Arrays.sort(dirs, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.length() == o2.length()){//按照字典序排序
                    return o1.compareTo(o2);
                }
                return o2.length() - o1.length();//按照字符串长度由大到小排序
            }
        });
        for (String str : dirs){
            int i = 0;
            for (char c : s.toCharArray()){
                if (i < str.length() && c == str.charAt(i)) i ++;
            }
            if (i == str.length()) return str;
        }
        return "";
    }
}

② 不需要排序的方法,我们遍历字典中的单词,然后还是跟上面的方法一样来验证当前单词是否能由字符串s通过删除字符来得到,如果能得到,而且单词长度大于等于结果res的长度,我们再看是否需要更新结果res,有两种情况是必须要更新结果res的,一个是当前单词长度大于结果res的长度,另一种是当前单词长度和res相同,但是字母顺序小于结果res,这两种情况下更新结果res即可。

class Solution { //44ms
    public String findLongestWord(String s, List<String> d) {
        String res = "";
        for (String str : d){
            int i = 0;
            for(char c : s.toCharArray()){
                if (i < str.length() && c == str.charAt(i)) i ++;
            }
            if (i == str.length() && str.length() >= res.length()){
                if (str.length() > res.length() || str.compareTo(res) < 0){
                    res = str;
                }
            }
        }
        return res;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1604365

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值