aboutsummaryrefslogtreecommitdiff
path: root/num_matching_subsequence.cpp
blob: a97d464e2d3f17d0920a11b8c69db7c4ebc2c6f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "leetcode.h"

class Solution {
public:
  int numMatchingSubseq(string s, vector<string> &words) {
    int ans = 0;
    unordered_map<string, int> up;
    for (int i = 0; i < words.size(); i++)
      up[words[i]]++;
    for (auto it = up.begin(); it != up.end(); it++)
      if (helper(s, it->first))
        ans += it->second;
    return ans;
  }

private:
  int helper(string a, string b) {
    int i = 0, j = 0, m = a.size(), n = b.size();
    while (i < m && j < n) {
      if (a[i] == b[j])
        i++, j++;
      else
        i++;
    }
    return (j == n);
  }
};

int main() {
  Solution obj;
  string s = "abcde";
  vector<string> words = {"a", "bb", "acd", "ace"};
  cout << obj.numMatchingSubseq(s, words);
}