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. Django的admin视图的使用

    要现在admin.py文件中将你要视图化操作的类进行注册: from django.contrib import admin from api import models # Register you ...

  2. 工厂方法模式-Factory Method(Java实现)

    工厂方法模式-Factory Method 工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法让实例化的具体内容交给子类工厂来进行. 本文中的例子是这样的. 生产一个身份证, ...

  3. java.lang.OutOfMemoryError: Java heap space内存不足问题

    今晚,在定义一个new int[19001][13001]的数组时候内存不够:特转了一下方法: Exception in thread "main" java.lang.OutOf ...

  4. DirectX11 With Windows SDK--12 深度/模板状态、平面镜反射绘制

    前言 深度/模板测试使用的是与后备缓冲区同等分辨率大小的缓冲区,每个元素的一部分连续位用于深度测试,其余的则用作模板测试.两个测试的目的都是为了能够根据深度/模板状态需求的设置来选择需要绘制的像素. ...

  5. SpringBoot系列: 所有配置属性和官方文档

    Spring Boot 通用配置参数https://docs.spring.io/spring-boot/docs/current/reference/html/common-application- ...

  6. Resources (being shared)

    论文下载求助论坛 数学杂志的模版 答辩PPT模版 发一篇文章的经历 数学期刊名称缩写 英文书籍下载 英文书籍下载 中文书籍下载 数学分析高等代数考研试题官方下载地址

  7. idea+maven+ssm搭建boot_crm项目遇到的问题

    使用idea+maven+ssm搭建一个boot_crm项目,遇到的问题如下: 1.环境搭建好了,相关配置文件都配置好了,也部署到了tomcat,但是无法启动首页. 通过百度,google找到了,原因 ...

  8. tomcat7的安装与maven安装

    tomcat7的安装与配置: 下载tomcat7 :wget 地址 解压:tar -zxvf 文件名 编辑tomcat目录下的conf下的server.xml文件: <Connector por ...

  9. NB-IoT不一定最完美 但足以成为决定ofo与摩拜物联网胜负的关键【转】

    转自:http://news.rfidworld.com.cn/2017_11/3d5ed5c5d8cb9949.html 2018年到来之前,如果还不懂物联网,你会被淘汰. 今年1月,工信部< ...

  10. 关于eclipse从svn导入项目

    第一次直接从eclipse svn资源库输入svn url直接将项目下载到workspace结果,web项目成了Java项目,这样操作是错误的 正确的操作应该是: file,import ,找到svn ...