前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Longest Word in Dictionary through Deleting

Longest Word in Dictionary through Deleting

作者头像
Tyan
发布2019-05-25 22:57:47
2820
发布2019-05-25 22:57:47
举报
文章被收录于专栏:SnailTyanSnailTyanSnailTyan

1. Description

Longest Word in Dictionary through Deleting
Longest Word in Dictionary through Deleting

2. Solution

  • No sort
class Solution {
public:
    string findLongestWord(string s, vector<string>& d) {
        int index = 0;
        int maxLength = 0;
        for(int i = 0; i <  d.size(); i++) {
            int m = 0;
            int n = 0;
            while(m < s.size() && n < d[i].size()) {
                if(s[m] == d[i][n]) {
                    n++;
                }
                m++;
            }
            if(n == d[i].size() && (maxLength < d[i].size() || (maxLength == d[i].size() && d[i] < d[index]))) {
                index = i;
                maxLength = d[i].size();
            }
        }
        if(maxLength == 0) {
            return "";
        }
        return d[index];
    }
};
  • Sort
bool compare(string& a,string& b)
{
      return a.size() != b.size()?a.size() > b.size():a < b;
}

class Solution {
public:
    string findLongestWord(string s, vector<string>& d) {
        sort(d.begin(), d.end(), compare);
        for(int i = 0; i <  d.size(); i++) {
            int m = 0;
            int n = 0;
            while(m < s.size() && n < d[i].size()) {
                if(s[m] == d[i][n]) {
                    n++;
                }
                m++;
            }
            if(n == d[i].size()) {
                return d[i];
            }
        }
        return "";
    }
};
  • Faster(use find function)
class Solution {
public:
    string findLongestWord(string s, vector<string>& d) {
        int index = 0;
        int maxLength = 0;
        for(int i = 0; i <  d.size(); i++) {
            int charIndex = -1;
            int j = 0;
            for(j = 0; j < d[i].size(); j++) {
                charIndex = s.find(d[i][j], charIndex + 1);
                if(charIndex == -1) {
                    break;
                }
            }
            if(j == d[i].size() && (maxLength < d[i].size() || (maxLength == d[i].size() && d[i] < d[index]))) {
                index = i;
                maxLength = d[i].size();
            }
        }
        if(maxLength == 0) {
            return "";
        }
        return d[index];
    }
};
  • Faster(use find function + sort)
bool compare(string& a,string& b)
{
      return a.size() != b.size()?a.size() > b.size():a < b;
}

class Solution {
public:
    string findLongestWord(string s, vector<string>& d) {
        sort(d.begin(), d.end(), compare);
        for(int i = 0; i <  d.size(); i++) {
            int charIndex = -1;
            int j = 0;
            for(j = 0; j < d[i].size(); j++) {
                charIndex = s.find(d[i][j], charIndex + 1);
                if(charIndex == -1) {
                    break;
                }
            }
            if(j == d[i].size()) {
                return d[i];
            }
        }
        return "";
    }
};

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年07月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Description
  • 2. Solution
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档