LeetCode 524. 通过删除字母匹配到字典里最长单词

文章目录

    • 一、题目描述
    • 二、解题思路
    • 三、代码

一、题目描述

1、题目描述
给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary 中最长的字符串,该字符串可以通过删除 s 中的某些字符得到。

如果答案不止一个,返回长度最长且字母序最小的字符串。如果答案不存在,则返回空字符串。

示例 1:
输入:s = “abpcplea”, dictionary = [“ale”,“apple”,“monkey”,“plea”]
输出:“apple”

示例 2:
输入:s = “abpcplea”, dictionary = [“a”,“b”,“c”]
输出:“a”

题目链接

二、解题思路

先将数组中字符串按题意排序,然后依次判断即可。

三、代码

class Solution {
    bool check(const string& s, const string& d) {
        int i = 0, j = 0;
        while(i < s.size() && j < d.size()) {
            if(s[i] == d[j]) {
                i++, j++;
            }
            else {
                i++;
            }
        }
        return j == d.size();
    }
public:
    string findLongestWord(string s, vector<string>& d) {
        sort(d.begin(), d.end(), [&](const string& a, const string& b) {
            if(a.size() == b.size()) {
                return a < b;
            }
            return a.size() > b.size();
        });
        for(int i = 0;i < d.size();i++) {
            if(check(s, d[i])) {
                return d[i];
            }
        }
        return "";
    }
};

你可能感兴趣的:(LeetCode算法刷题,c++,算法)