首页 > 其他分享 >LeetCode 792. Number of Matching Subsequences

LeetCode 792. Number of Matching Subsequences

时间:2022-10-06 12:33:16浏览次数:73  
标签:subsequence Number char item waiting Subsequences words new Matching

原题链接在这里:https://leetcode.com/problems/number-of-matching-subsequences/

题目:

Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s.

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

  • For example, "ace" is a subsequence of "abcde".

Example 1:

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

Example 2:

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

Constraints:

  • 1 <= s.length <= 5 * 104
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 50
  • s and words[i] consist of only lowercase English letters.

题解:

For each word in words, its first char of each word is the waiting char.

When iterate s, the currrent char c release corresponding waiting group in words, remove it and see the next waiting char.

If there is none, then we find a subsequence, res++.

To make sure it is liner, we use LinkedList<Character> to represent String.

Time Complexity: O(m*n + len). m = words.length. n is word length. len = s.length().

Space: O(m*n + len).

AC Java:

 1 class Solution {
 2     public int numMatchingSubseq(String s, String[] words) {
 3         int res = 0;
 4         List<LinkedList<Character>> [] waiting = new List[26];
 5         for(int i = 0; i < 26; i++){
 6             waiting[i] = new ArrayList<LinkedList<Character>>();
 7         }
 8         
 9         for(String w : words){
10             LinkedList<Character> que = new LinkedList<>();
11             for(char letter : w.toCharArray()){
12                 que.add(letter);
13             }
14             
15             waiting[w.charAt(0) - 'a'].add(que);
16         }
17         
18         for(char c : s.toCharArray()){
19             List<LinkedList<Character>> cur = waiting[c - 'a'];
20             waiting[c - 'a'] = new ArrayList<LinkedList<Character>>();
21             for(LinkedList<Character> item : cur){
22                 item.pollFirst();
23                 if(item.size() == 0){
24                     res++;
25                 }else{
26                     waiting[item.peekFirst() - 'a'].add(item);
27                 }
28             }
29         }
30         
31         return res;
32     }
33 }

 

标签:subsequence,Number,char,item,waiting,Subsequences,words,new,Matching
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16757399.html

相关文章

  • LeetCode 1419. Minimum Number of Frogs Croaking
    原题链接在这里:https://leetcode.com/problems/minimum-number-of-frogs-croaking/题目:Youaregiventhestring croakOfFrogs,whichrepresentsacombinationofth......
  • IfcComplexNumber
    IfcComplexNumber类型定义IfcComplexNumber是复数的表示形式,表示为具有两个元素的数组。第一个元素(索引1)表示实数分量,实数分量是复数的数值分量,复数的平方根可以明确计算......
  • Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 106; 对
    给Properties注入值报错<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2......
  • HDU3652 B-number
    B-number题意:求1-n(<=1e9)中是13的倍数且包含“13”的个数。多组数据。数位DP#include<bits/stdc++.h>usingnamespacestd;intn;intdat[15],cnt;intf[13][11......
  • Codeforces Global Round 22 C. Even Number Addicts(博弈论)
    https://codeforces.com/contest/1738/problem/C题目大意:给定n个数字,Alice先手,Bob后手;拿完之后,Alice数字总和为奇数时Alice获胜,否则Bob获胜。问我们给定n个数字,双方......
  • [Oracle] LeetCode 2 Add Two Numbers
    Youaregiventwonon-emptylinkedlistsrepresentingtwonon-negativeintegers.Thedigitsarestoredinreverseorder,andeachoftheirnodescontainsasin......
  • POJ 2247 Humble Numbers(搜索,生成子集)
    HumbleNumbers(搜索,生成子集)题目:​ 给出多次询问,问第k个丑数是多少(最多询问到k=5842)。​ 丑数:分解质因数后,质因子只包含2,3,5,7的数字。思路:​ 通过预处理得到,584......
  • P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
    这是一道动态规划的经典入门题,重点在于递规过程中存储计算结果,避免重复计算.当然直接简单粗暴使用递归也可以拿到部分分数.只是样例太大的话就过不了了.题目描述观......
  • CF 617E XOR and Favorite Number 题解
    如果我们对给定的序列求出异或意义下的前缀和,哪么题意变为求满足\(sum_{i-1}\operatorname{xor}sum_j=k\)的\((i,j)\)数量。由于\(x\operatorname{xor}y=z\)等......
  • Even Number Addicts - 题解【动态规划/记忆化搜索】
    题面本题是CodeforcesGlobalRound22的C题。原题链接见:C.EvenNumberAddicts。下面搬运一下题面:DescriptionAliceandBobareplayingagameonasequence\(a_......