leetcode 792 Number of words matching subsequence (hash table + double pointer)

792. Number of Words Matching Subsequence
medium
282
related business

Given a string  s and an array of strings  words, returns   the number of words in the subsequence of words[i] iss  .

A subsequence of a string   is a new string generated from the original string from which some characters (could be none) can be deleted without changing the relative order of the remaining characters.

  • For example,  “ace” is  “abcde” a subsequence of .

 

Example 1:

Input: s = "abcde", words = ["a","bb","acd","ace"] Output: 3 Explanation: There are three words that are subsequences of s: "a", "acd", "ace". 

Example 2:

Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"] Output: 2 

 

hint:

  • 1 <= s.length <= 5 * 104
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 50
  • words[i]Both s and  s  consist of lowercase letters only.
​​​​
Passes
23.7K
Submissions
47.5K
Passing rate
49.9%

Problem solution: compare two strings s and t, one of which is another substring problem, you can use double pointers to traverse at one time. Then this question is to add a layer of for loop outside, and it is enough to traverse all the string arrays. So I submitted it and found that it timed out. . . . Now that the timeout has expired, the algorithm needs to be optimized to delete some repeated operations. Looking at the timeout test case, I suddenly understood, because many of the test strings are repeated, so I added a hash table to remove the duplication, so that I can pass through the hash table again. Looking at the code specifically, using table[26] is an optimization used to end mismatches early.

class Solution {
public:
    int numMatchingSubseq(string s, vector<string>& words) {
        int sum=0;
        int table[26]={0};
        unordered_map<string,int> Hashmap;
        for(auto ch:s)
        {
            table[ch-'a']++;
        }
        for(auto str:words)
        {
            Hashmap[str]++;
        }
        for(auto p:Hashmap)
        {
            string temStr=p.first;
            int k=0;
            int sk=0;
            // 计算是否存在
            bool flag=true;
            int temptable[26]={0};
            for(auto i:temStr)
            {
                temptable[i-'a']++;
                if(temptable[i-'a']>table[i-'a'])
                {
                    flag=false;
                    break;
                }
            }
            if(flag)
            {
                while(k<temStr.size() && sk<s.size())
                {
                    if(temStr[k]==s[sk])
                    {
                        k++;
                        sk++;
                    }
                    else
                    {
                        sk++;
                    }
                }
                if(k>temStr.size()-1)
                {
                    sum+=p.second;
                }
            }
        }
        return sum;
    }
};

Time 168 ms

beat

63.30%

RAM42.1 MB

beat

74.64%

Guess you like

Origin blog.csdn.net/weixin_41579872/article/details/127902244