lintcode 284. 匹配子序列的单词

给定字符串 source 和单词字典 words, 返回所有 words 中是 source 的子序列的单词。

样例
示例 1:
输入: 
source = "abcde"
words = ["a", "bb", "acd", "ace"]
输出: ["a", "acd", "ace"]
解释: 有三个是 source 的子序列的单词: "a", "acd", "ace"。
示例2 :
输入: 
source = "bcogtadsjofisdhklasdj"
word = ["book","code","tag"]
输出: ["book"]
解释: 仅有一个是 source 的子序列的单词: "book"
挑战
source的长度范围是 [1, 100000].
words中所有单词长度之和的长度范围是 [1, 1000].
注意事项
source 和 words 中的字符保证都是大小写字母组成的
source的长度范围是 [1, 1000].
words中所有单词长度之和的长度范围是 [1, 1000].
words中包含的单词个数范围是 [1, 1000].
class Solution {
public:
    /**
     * @param source: A string
     * @param words: A list of string
     * @return: return list of words[i] that is a subsequence of source.
     */
    vector<string> MatchingSubsequences(string &source, vector<string> &words) {
        // write your code here
        int slen=source.size();
        int wlen=words.size();
        vector<int>count(wlen,0);
        for (int i = 0; i < slen; i++) {
            /* code */
            for(int j = 0; j < wlen; j++)
            {
                if(count[j]==INT_MAX) continue;
                if(source[i]==words[j][count[j]]) count[j]++;
                if(count[j]==words[j].size()) count[j]=INT_MAX;
            }
        }
        vector<string> res;
        for (int i = 0; i < wlen; i++) {
            /* code */
            if(count[i]==INT_MAX) res.push_back(words[i]);
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值