英文原文
Sometimes people repeat letters to represent extra feeling. For example:
"hello" -> "heeellooo""hi" -> "hiiii"
In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".
You are given a string s and an array of query strings words. A query word is stretchy if it can be made to be equal to s by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is three or more.
- For example, starting with
"hello", we could do an extension on the group"o"to get"hellooo", but we cannot get"helloo"since the group"oo"has a size less than three. Also, we could do another extension like"ll" -> "lllll"to get"helllllooo". Ifs = "helllllooo", then the query word"hello"would be stretchy because of these two extension operations:query = "hello" -> "hellooo" -> "helllllooo" = s.
Return the number of query strings that are stretchy.
Example 1:
Input: s = "heeellooo", words = ["hello", "hi", "helo"] Output: 1 Explanation: We can extend "e" and "o" in the word "hello" to get "heeellooo". We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.
Example 2:
Input: s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"] Output: 3
Constraints:
1 <= s.length, words.length <= 1001 <= words[i].length <= 100sandwords[i]consist of lowercase letters.
中文题目
有时候人们会用重复写一些字母来表示额外的感受,比如 "hello" -> "heeellooo", "hi" -> "hiii"。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h", "eee", "ll", "ooo"。
对于一个给定的字符串 S ,如果另一个单词能够通过将一些字母组扩张从而使其和 S 相同,我们将这个单词定义为可扩张的(stretchy)。扩张操作定义如下:选择一个字母组(包含字母 c ),然后往其中添加相同的字母 c 使其长度达到 3 或以上。
例如,以 "hello" 为例,我们可以对字母组 "o" 扩张得到 "hellooo",但是无法以同样的方法得到 "helloo" 因为字母组 "oo" 长度小于 3。此外,我们可以进行另一种扩张 "ll" -> "lllll" 以获得 "helllllooo"。如果 S = "helllllooo",那么查询词 "hello" 是可扩张的,因为可以对它执行这两种扩张操作使得 query = "hello" -> "hellooo" -> "helllllooo" = S。
输入一组查询单词,输出其中可扩张的单词数量。
示例:
输入: S = "heeellooo" words = ["hello", "hi", "helo"] 输出:1 解释: 我们能通过扩张 "hello" 的 "e" 和 "o" 来得到 "heeellooo"。 我们不能通过扩张 "helo" 来得到 "heeellooo" 因为 "ll" 的长度小于 3 。
提示:
0 <= len(S) <= 100。0 <= len(words) <= 100。0 <= len(words[i]) <= 100。S和所有在words中的单词都只由小写字母组成。
通过代码
官方题解
比较相同字母组的长度:
我们首先将 S 拆分成若干组相同的字母,并存储每组字母的长度。例如当 S 为 abbcccddddaaaaa 时,可以得到 5 组字母,它们分别为 abcda,长度为 [1, 2, 3, 4, 5]。
对于 words 中的每个单词 word,如果它可以扩张得到 S,那么它必须和 S 有相同的字母组。对于每一组字母,假设 S 中有 c1 个,word 中有 c2 个,那么会有下面几种情况:
如果
c1 < c2,那么word不能扩张得到S;如果
c1 >= 3,那么只要添加c1 - c2个字母即可;如果
c1 < 3,由于在扩张时至少需要添加到3个字母,所以此时不能添加字母,必须有c1 == c2。
如果 word 的包含的字母组中的每个字母都满足上述情况,那么 word 可以扩张得到 S。
[sol1]class Solution { public int expressiveWords(String S, String[] words) { RLE R = new RLE(S); int ans = 0; search: for (String word: words) { RLE R2 = new RLE(word); if (!R.key.equals(R2.key)) continue; for (int i = 0; i < R.counts.size(); ++i) { int c1 = R.counts.get(i); int c2 = R2.counts.get(i); if (c1 < 3 && c1 != c2 || c1 < c2) continue search; } ans++; } return ans; } } class RLE { String key; List<Integer> counts; public RLE(String S) { StringBuilder sb = new StringBuilder(); counts = new ArrayList(); char[] ca = S.toCharArray(); int N = ca.length; int prev = -1; for (int i = 0; i < N; ++i) { if (i == N-1 || ca[i] != ca[i+1]) { sb.append(ca[i]); counts.add(i - prev); prev = i; } } key = sb.toString(); } }
[sol1]class Solution(object): def expressiveWords(self, S, words): def RLE(S): return zip(*[(k, len(list(grp))) for k, grp in itertools.groupby(S)]) R, count = RLE(S) ans = 0 for word in words: R2, count2 = RLE(word) if R2 != R: continue ans += all(c1 >= max(c2, 3) or c1 == c2 for c1, c2 in zip(count, count2)) return ans
复杂度分析
时间复杂度:$O(N + \sum k_i)$,其中 $N$ 是字符串
S的长度,$\sum k_i$ 是数组words中所有单词的长度之和。空间复杂度:$O(N + K)$,其中
K是数组word中最长的单词的长度。
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 5121 | 12190 | 42.0% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|