题目形容

这是 LeetCode 上的 809. 情感丰盛的文字 ,难度为 中等

Tag : 「双指针」、「模仿」

有时候人们会用反复写一些字母来示意额定的感触,比方 "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 中的单词都只由小写字母组成。

双指针

该题最难的局部就是了解 “扩张” 操作:假如有两个字符雷同的间断段 ab,如何判断 a 是否能由 b 扩张而来。

忘记掉题目所说的规定,咱们从新定义 “扩张” 操作:

  • ab 长度雷同,定义为可扩张;
  • ab 长度不同,依据「ab 长度比照」以及「a 的长度大小」分状况探讨:

    • b 长度大于 a,不可扩张;
    • a 长度大于 b咱们不肯定要拿整一段的 b 进行扩张,能够拿 b 中的一个字符进行扩张。 因而只须要满足扩张后的长度(a 的长度)大于 $3$ 即可定义为可扩张。

搞明确何为 “扩张” 后,残余的则是简略的「双指针 + 模仿」做法。

Java 代码:

class Solution {    public int expressiveWords(String s, String[] words) {        int n = s.length(), ans = 0;        out:for (String word : words) {            int m = word.length(), i = 0, j = 0;            while (i < n && j < m) {                if (s.charAt(i) != word.charAt(j)) continue out;                int a = i, b = j;                while (a < n && s.charAt(a) == s.charAt(i)) a++;                while (b < m && word.charAt(b) == word.charAt(j)) b++;                a -= i; b -= j;                if (a != b && (b > a || a < 3)) continue out;                                i += a; j += b;            }            if (i == n && j == m) ans++;        }        return ans;    }}

TypeScript 代码:

function expressiveWords(s: string, words: string[]): number {    let n = s.length, ans = 0    out:for (const word of words) {        let m = word.length, i = 0, j = 0        while (i < n && j < m) {            if (s[i] != word[j]) continue out            let a = i, b = j            while (a < n && s[a] == s[i]) a++            while (b < m && word[b] == word[j]) b++            a -= i; b -= j;            if (a != b && (b > a || a < 3)) continue out            i += a; j += b;        }        if (i == n && j == m) ans++;    }    return ans}

Python 代码:

class Solution:    def expressiveWords(self, s: str, words: List[str]) -> int:        n, ans = len(s), 0        for word in words:            m, i, j = len(word), 0, 0            ok = True            while ok and i < n and j < m:                if s[i] != word[j]:                    ok = False                a, b = i, j                while a < n and s[a] == s[i]:                    a += 1                while b < m and word[b] == word[j]:                    b += 1                a, b = a - i, b - j                if a != b and (b > a or a < 3):                    ok = False                i, j = i + a, j + b            if ok and i == n and j == m:                ans += 1        return ans
  • 工夫复杂度:$O(n \times m + \sum_{i = 0}^{m - 1}words[i].length)$,其中 n 为字符串 s 的长度,m 为数组 words 的长度
  • 空间复杂度:$O(1)$

最初

这是咱们「刷穿 LeetCode」系列文章的第 No.809 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,局部是有锁题,咱们将先把所有不带锁的题目刷完。

在这个系列文章外面,除了解说解题思路以外,还会尽可能给出最为简洁的代码。如果波及通解还会相应的代码模板。

为了不便各位同学可能电脑上进行调试和提交代码,我建设了相干的仓库:https://github.com/SharingSou... 。

在仓库地址里,你能够看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其余优选题解。

更多更全更热门的「口试/面试」相干材料可拜访排版精美的 合集新基地

本文由mdnice多平台公布