[LeetCode] 809. Expressive Words
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 <= 100
1 <= words[i].length <= 100
s
andwords[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。
输入一组查询单词,输出其中可扩张的单词数量。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/expressive-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题的思路是双指针,类似第二题。对于 words 里的每一个单词,我们拿出来和 s 做一下双指针的比较。我们每遇到一个字母的时候,我们去看一下当前这个字母最多连续出现几次。因为题目中说的可扩张的定义是某个字母在s中出现一次,就要在word中连续出现至少三次。照着这个规则,我们去判断word是否合法。
时间O(mn) - m 个单词,每个单词平均长度 n
空间O(1)
Java实现
1 class Solution { 2 public int expressiveWords(String s, String[] words) { 3 // corner case 4 if (s == null || s.length() == 0 || words == null || words.length == 0) { 5 return 0; 6 } 7 8 // normal case 9 int count = 0; 10 for (String word : words) { 11 if (helper(s, word)) { 12 count++; 13 } 14 } 15 return count; 16 } 17 18 private boolean helper(String s, String word) { 19 int len1 = s.length(); 20 int len2 = word.length(); 21 if (len1 < len2) { 22 return false; 23 } 24 int i = 0, j = 0; 25 while (i < len1 && j < len2) { 26 char c1 = s.charAt(i); 27 char c2 = word.charAt(j); 28 int count1 = 0; 29 int count2 = 0; 30 while (i < len1 && s.charAt(i) == c1) { 31 i++; 32 count1++; 33 } 34 while (j < len2 && word.charAt(j) == c2) { 35 j++; 36 count2++; 37 } 38 if (c1 != c2 || count1 < count2 || count1 <= 2 && count1 != count2) { 39 return false; 40 } 41 } 42 return i == len1 && j == len2; 43 } 44 }