[LeetCode] Expressive Words 富于表现力的单词
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.Sand all words inwordsconsist 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;
}
};
参考资料:
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Expressive Words 富于表现力的单词的更多相关文章
- [LeetCode] Concatenated Words 连接的单词
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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 ...
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters
地址 https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/ 题目描述你将会得到一份单词表 words,一个字母 ...
- [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 ...
随机推荐
- js值类型转换(boolean/String/number),js运算符,if条件,循环结构,函数,三种弹出框
js值类型转换 number | string | boolean boolean类型转换 num = 0; var b1 = Boolean(num); console.log(b1) 转化为数字类 ...
- JavaScript数据类型 Boolean布尔类型
前言 布尔值Boolean类型可能是三种包装对象Number.String和Boolean中最简单的一种.Number和String对象拥有大量的实例属性和方法,Boolean却很少.从某种意义上说, ...
- MySQL学习1 - 基本mysql语句
一 操作文件夹(数据库) 增 查 改 删 二 操作文件(数据表) 增 查 改 删 三 操作文件内容(数据记录) 增 查 改 删 一 操作文件夹(数据库) 增 create database db1 c ...
- rem是怎么计算的
「rem」是指根元素(root element,html)的字体大小,从遥远的 IE6 到版本到 Chrome 他们都约好了,根元素默认的 font-size 都是 16px. rem是通过根元素进行 ...
- L1-Day4
L1-Day4 1.这消息使她非常悲伤. [我的翻译]The message makes she very sad. [标准答案]The news made her very sad. [对比分析]( ...
- Distance on the tree(数剖 + 主席树)
题目链接:https://nanti.jisuanke.com/t/38229 题目大意:给你n个点,n-1条边,然后是m次询问,每一次询问给你u,v,w然后问你从u -> v 的路径上有多少边 ...
- java 面经
1.什么是Java虚拟机(JVM)?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. ...
- TCP/IP(五)传输层之细说TCP的三次握手和四次挥手
前言 这一篇我将介绍的是大家面试经常被会问到的,三次握手四次挥手的过程.以前我听到这个是什么意思呀?听的我一脸蒙逼,但是学习之后就原来就那么回事! 一.运输层概述 1.1.运输层简介 这一层的功能也挺 ...
- 【原创】大叔经验分享(13)spark运行报错WARN Utils: Service 'sparkDriver' could not bind on port 0. Attempting port 1.
本地运行spark报错 18/12/18 12:56:55 WARN Utils: Service 'sparkDriver' could not bind on port 0. Attempting ...
- 适合前端学习JS的网站
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript