Sometimes people repeat letters to represent extra feeling, such as "hello" -> "heeellooo", "hi" -> "hiiii". In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".
For some given string S, 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 3 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 size less than 3. Also, we could do another extension like "ll" -> "lllll" to get "helllllooo". If S = "helllllooo", then the query word "hello" would be stretchy because of these two extension operations: query = "hello" -> "hellooo" -> "helllllooo" = S.
Given a list of query words, return the number of words that are stretchy.
Example: 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.
Constraints:
0 <= len(S) <= 100.0 <= len(words) <= 100.0 <= len(words[i]) <= 100.Sand all words inwordsconsist only of lowercase letters
-----------------------------------------------------
The codes are not hard. Paste a simple and clear version using 4 pointers:
class Solution:
def expressiveWords(self, S: str, words) -> int:
def check(S, W):
i, j, i2, j2, n, m = 0, 0, 0, 0, len(S), len(W)
while i < n and j < m:
if S[i] != W[j]: return False
while i2 < n and S[i2] == S[i]: i2 += 1
while j2 < m and W[j2] == W[j]: j2 += 1
if i2 - i != j2 - j and i2 - i < max(3, j2 - j): return False
i, j = i2, j2
return i == n and j == m
return len([word for word in words if (check(S,word))])

本文介绍了一种判断字符串是否可以通过特定规则拉伸匹配另一种字符串的算法。通过实例演示了如何检查一个单词是否能通过增加字符组达到指定长度的字符串,如hello拉伸为heeellooo。探讨了算法实现细节,包括使用四个指针进行比较的方法。

被折叠的 条评论
为什么被折叠?



