|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[792. 匹配子序列的单词数](https://leetcode.cn/problems/number-of-matching-subsequences/solution/by-ac_oier-u1ox/)** ,难度为 **中等**。 |
| 4 | + |
| 5 | +Tag : 「二分」、「哈希表」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +给定字符串 `s` 和字符串数组 `words`, 返回 `words[i]` 中是 `s` 的子序列的单词个数 。 |
| 10 | + |
| 11 | +字符串的 子序列 是从原始字符串中生成的新字符串,可以从中删去一些字符(可以是`""`),而不改变其余字符的相对顺序。 |
| 12 | + |
| 13 | +例如, `"ace"` 是 `"abcde"` 的子序列。 |
| 14 | + |
| 15 | +示例 1: |
| 16 | +``` |
| 17 | +输入: s = "abcde", words = ["a","bb","acd","ace"] |
| 18 | + |
| 19 | +输出: 3 |
| 20 | + |
| 21 | +解释: 有三个是 s 的子序列的单词: "a", "acd", "ace"。 |
| 22 | +``` |
| 23 | +示例 2: |
| 24 | +``` |
| 25 | +输入: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"] |
| 26 | + |
| 27 | +输出: 2 |
| 28 | +``` |
| 29 | + |
| 30 | +提示: |
| 31 | +* 1ドル <= s.length <= 5 \times 10^4$ |
| 32 | +* 1ドル <= words.length <= 5000$ |
| 33 | +* 1ドル <= words[i].length <= 50$ |
| 34 | +* `words[i]` 和 `s` 都只由小写字母组成。 |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +### 预处理 + 哈希表 + 二分 |
| 39 | + |
| 40 | +朴素判定某个字符串是为另一字符串的子序列的复杂度为 $O(n + m),ドル对于本题共有 5000ドル$ 个字符串需要判定,每个字符串最多长为 50ドル,ドル因此整体计算量为 $(5 \times 10^4 + 50) \times 5000 \approx 2.5 \times 10^8,ドル会超时。 |
| 41 | + |
| 42 | +不可避免的是,我们要对每个 $words[i]$ 进行检查,因此优化的思路可放在如何优化单个 $words[i]$ 的判定操作。 |
| 43 | + |
| 44 | +朴素的判定过程需要使用双指针扫描两个字符串,其中对于原串的扫描,会有大量的字符会被跳过(无效匹配),即只有两指针对应的字符相同时,匹配串指针才会后移。 |
| 45 | + |
| 46 | +我们考虑如何优化这部分无效匹配。 |
| 47 | + |
| 48 | +对于任意一个 $w = words[i]$ 而言,假设我们当前匹配到 $w[j]$ 位置,此时我们已经明确下一个待匹配的字符为 $w[j + 1],ドル因此我们可以直接在 `s` 中字符为 $w[j + 1]$ 的位置中找候选。 |
| 49 | + |
| 50 | +具体的,我们可以使用哈希表 `map` 对 `s` 进行预处理:以字符 $c = s[i]$ 为哈希表的 `key`,对应的下标 $i$ 集合为 `value`,由于我们从前往后处理 `s` 进行预处理,因此对于所有的 `value` 均满足递增性质。 |
| 51 | + |
| 52 | +> 举个 🌰 : 对于 `s = abcabc` 而言,预处理的哈希表为 `{a=[0,3], b=[1,4], c=[2,5]}` |
| 53 | + |
| 54 | +最后考虑如何判定某个 $w = words[i]$ 是否满足要求:待匹配字符串 `w` 长度为 `m`,我们从前往后对 `w` 进行判定,假设当前判待匹配位置为 $w[i],ドル我们使用变量 `idx` 代表能够满足匹配 $w[0:i]$ 的最小下标(贪心思路)。 |
| 55 | + |
| 56 | +对于匹配的 $w[i]$ 字符,可以等价为在 `map[w[i]]` 中找到第一个大于 `idx` 的下标,含义在原串 `s` 中找到字符为 `w[i]` 且下标大于 `idx` 的最小值,由于我们所有的 `map[X]` 均满足单调递增,该过程可使用「二分」进行。 |
| 57 | + |
| 58 | +Java 代码: |
| 59 | +```Java |
| 60 | +class Solution { |
| 61 | + public int numMatchingSubseq(String s, String[] words) { |
| 62 | + int n = s.length(), ans = 0; |
| 63 | + Map<Character, List<Integer>> map = new HashMap<>(); |
| 64 | + for (int i = 0; i < n; i++) { |
| 65 | + List<Integer> list = map.getOrDefault(s.charAt(i), new ArrayList<>()); |
| 66 | + list.add(i); |
| 67 | + map.put(s.charAt(i), list); |
| 68 | + } |
| 69 | + for (String w : words) { |
| 70 | + boolean ok = true; |
| 71 | + int m = w.length(), idx = -1; |
| 72 | + for (int i = 0; i < m && ok; i++) { |
| 73 | + List<Integer> list = map.getOrDefault(w.charAt(i), new ArrayList<>()); |
| 74 | + int l = 0, r = list.size() - 1; |
| 75 | + while (l < r) { |
| 76 | + int mid = l + r >> 1; |
| 77 | + if (list.get(mid) > idx) r = mid; |
| 78 | + else l = mid + 1; |
| 79 | + } |
| 80 | + if (r < 0 || list.get(r) <= idx) ok = false; |
| 81 | + else idx = list.get(r); |
| 82 | + } |
| 83 | + if (ok) ans++; |
| 84 | + } |
| 85 | + return ans; |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | +TypeScript 代码: |
| 90 | +```TypeScript |
| 91 | +function numMatchingSubseq(s: string, words: string[]): number { |
| 92 | + let n = s.length, ans = 0 |
| 93 | + const map = new Map<String, Array<number>>() |
| 94 | + for (let i = 0; i < n; i++) { |
| 95 | + if (!map.has(s[i])) map.set(s[i], new Array<number>()) |
| 96 | + map.get(s[i]).push(i) |
| 97 | + } |
| 98 | + for (const w of words) { |
| 99 | + let ok = true |
| 100 | + let m = w.length, idx = -1 |
| 101 | + for (let i = 0; i < m && ok; i++) { |
| 102 | + if (!map.has(w[i])) { |
| 103 | + ok = false |
| 104 | + } else { |
| 105 | + const list = map.get(w[i]) |
| 106 | + let l = 0, r = list.length - 1 |
| 107 | + while (l < r) { |
| 108 | + const mid = l + r >> 1 |
| 109 | + if (list[mid] > idx) r = mid |
| 110 | + else l = mid + 1 |
| 111 | + } |
| 112 | + if (r < 0 || list[r] <= idx) ok = false |
| 113 | + else idx = list[r] |
| 114 | + } |
| 115 | + } |
| 116 | + if (ok) ans++ |
| 117 | + } |
| 118 | + return ans |
| 119 | +} |
| 120 | +``` |
| 121 | +Python3 代码: |
| 122 | +```Python3 |
| 123 | +class Solution: |
| 124 | + def numMatchingSubseq(self, s: str, words: List[str]) -> int: |
| 125 | + dmap = defaultdict(list) |
| 126 | + for i, c in enumerate(s): |
| 127 | + dmap[c].append(i) |
| 128 | + ans = 0 |
| 129 | + for w in words: |
| 130 | + ok = True |
| 131 | + idx = -1 |
| 132 | + for i in range(len(w)): |
| 133 | + idxs = dmap[w[i]] |
| 134 | + l, r = 0, len(idxs) - 1 |
| 135 | + while l < r : |
| 136 | + mid = l + r >> 1 |
| 137 | + if dmap[w[i]][mid] > idx: |
| 138 | + r = mid |
| 139 | + else: |
| 140 | + l = mid + 1 |
| 141 | + if r < 0 or dmap[w[i]][r] <= idx: |
| 142 | + ok = False |
| 143 | + break |
| 144 | + else: |
| 145 | + idx = dmap[w[i]][r] |
| 146 | + ans += 1 if ok else 0 |
| 147 | + return ans |
| 148 | +``` |
| 149 | +* 时间复杂度:令 `n` 为 `s` 长度,`m` 为 `words` 长度,`l = 50` 为 $words[i]$ 长度的最大值。构造 `map` 的复杂度为 $O(n)$;统计符合要求的 $words[i]$ 的数量复杂度为 $O(m \times l \times \log{n})$。整体复杂度为 $O(n + m \times l \times \log{n})$ |
| 150 | +* 空间复杂度:$O(n)$ |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +### 最后 |
| 155 | + |
| 156 | +这是我们「刷穿 LeetCode」系列文章的第 `No.792` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 157 | + |
| 158 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 159 | + |
| 160 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 161 | + |
| 162 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 163 | + |
0 commit comments