Sometimes people repeat letters to represent extra feeling, such as "hello" -> "heeellooo", "hi" -> "hiiii".  Here, we have groups, of adjacent letters that are all the same character, and adjacent characters to the group are different.  A group is extended if that group is length 3 or more, so "e" and "o" would be extended in the first example, and "i" would be extended in the second example.  As another example, the groups of "abbcccaaaa" would be "a", "bb", "ccc", and "aaaa"; and "ccc" and "aaaa" are the extended groups of that string.

For some given string S, a query word is stretchy if it can be made to be equal to S by extending some groups.  Formally, we are allowed to repeatedly choose a group (as defined above) of characters c, and add some number of the same character c to it so that the length of the group is 3 or more.  Note that we cannot extend a group of size one like "h" to a group of size two like "hh" - all extensions must leave the group extended - ie., at least 3 characters long.

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 extended.

Notes:

  • 0 <= len(S) <= 100.
  • 0 <= len(words) <= 100.
  • 0 <= len(words[i]) <= 100.
  • S and all words in words consist only of lowercase letters

这道题定义了一种富于表现力的单词,就是说某个字母可以重复三次或以上,那么对于这种重复后的单词,我们称之为可拉伸的(stretchy)。现在给了我们一个拉伸后的单词S,又给了我们一个单词数组,问我们里面有多少个单词可以拉伸成为S。其实这道题的关键就在于看某个字母是否被重复了三次,重复两次是不行的。那么我们就只能遍历单词数组words中的单词,来分别和S比较了。每个遍历到的单词的长度suppose是应该小于等于S的,因为S是拉伸后的单词,当然S也可以和遍历到的单词相等,那么表示没有拉伸。我们需要两个指针i和j来分别指向S和遍历单词word,我们需要逐个比较,由于S的长度要大于等于word,所以我们for循环直接遍历S的字母就好了,首先看如果j没越界,并且此时S[i]和word[j]相等的话,那么j自增1,i在for循环中也会自增1,遍历下一个字母。如果此时不相等或者j已经越界的话,我们再看当前的S[i]是否是3个重复中的中间那个,即S[i-1]和S[i+1]需要等于S[i],是的话,i自增1,然后加上for循环中的自增1,相当于总共增了2个,正好跳过这个重复三连。否则的话再看是否前两个都和当前的字母相等,即S[i-1]和S[i-2]需要等于S[i],因为可能重复的个数多于3个,如果这个条件不满足的话,直接break就行了。for循环结束或者跳出后,我们看S和word是否正好遍历完,即i和j是否分别等于S和word的长度,是的话结果res自增1,参见代码如下:

class Solution {
public:
int expressiveWords(string S, vector<string>& words) {
int res = , m = S.size(), n = words.size();
for (string word : words) {
int i = , j = ;
for (; i < m; ++i) {
if (j < word.size() && S[i] == word[j]) ++j;
else if (i > && S[i] == S[i - ] && i + < m && S[i] == S[i + ]) ++i;
else if (!(i > && S[i] == S[i - ] && S[i] == S[i - ])) break;
}
if (i == m && j == word.size()) ++res;
}
return res;
}
};

参考资料:

https://leetcode.com/problems/expressive-words/discuss/122660/C++JavaPython-2-Pointers-and-4-pointers

https://leetcode.com/problems/expressive-words/discuss/121741/Short-straight-forward-C++-solution-two-pointers-one-pass-scan

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Expressive Words 富于表现力的单词的更多相关文章

  1. [LeetCode] Concatenated Words 连接的单词

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  2. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  3. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  4. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  5. [LeetCode] Short Encoding of Words 单词集的短编码

    Given a list of words, we may encode it by writing a reference string S and a list of indexes A. For ...

  6. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  7. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  8. LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters

    地址 https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/ 题目描述你将会得到一份单词表 words,一个字母 ...

  9. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

随机推荐

  1. docker 安装入门

    install docker 命令 docker version // docker 版本 docker pull nginx // 拉取nginx docker images // 查看本机dock ...

  2. SQL修改日期类型字段为字符串类型

    select * from test1 --添加行 ) --将转换格式后的数据放到列中 ) --删除老的字段 alter table test1 drop column startdate --修改字 ...

  3. jQuery1.9及以上版本检测IE版本号

    jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更新的 2.0 版本中,将不再支持 IE 6/7/8. ...

  4. python学习02

    python的数据类型 程序=数据类型+算法 1.数据类型:数据型,字符串,列表list,字典dict,set集合(),tuple元组() 1)数据型 int,整数型,理论上是无限大,不过受到机器内存 ...

  5. 9、el表达式的使用

    一.EL表达式的作用: 1).使用变量访问web域中存储的对象 ${user } 2).访问javabean的属性   ${user.address.city } 3).执行基本的逻辑运算(el表达式 ...

  6. 第三章 Java的基础程序设计结构

    一个简单的 Java 应用程序 访问修饰符 public,private,protected main 方法必须时public修饰的,C#则不必须 数据类型 可以用16进制表示浮点数 可以用2,8,1 ...

  7. Spring系列(三) Bean装配的高级技术

    profile 不同于maven的profile, spring的profile不需要重新打包, 同一个版本的包文件可以部署在不同环境的服务器上, 只需要激活对应的profile就可以切换到对应的环境 ...

  8. ModuleNotFoundError: No module named 'video_back.urls'

    新建Django项目时将settings,urls移除来时报错. 这是我所想要的项目结构  >>>  扁平结构. 将下面这个应用的名字删掉就可以了.

  9. 【原创】大数据基础之Ambari(3)通过Ambari部署Airflow

    ambari2.7.3(hdp3.1) 安装 airflow1.10 ambari的hdp中原生不支持airflow安装,下面介绍如何通过mpack方式使ambari支持airflow安装: 1 下载 ...

  10. 布思算法Java实现

    public String multiply(String Q,String M){ char Q0 = '0'; String A = get01(Q.length(),"0") ...