leetcode笔记(五)809. 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
- 解题思路
题目描述挺多~题目属于第一眼简单题,知道属于字符串匹配,知道最好用正则表达式。但是无奈记不清C++ regex类的用法。
所以,就退而求其次,用最笨的思路实现下字符串匹配。主要思路:
- 建立规则,根据标准字符串S,计算字符串匹配的规则,表示为字母和字母次数的pair。如
S = "heeellooo" --> [<h, 1>, <e, 3>, <l, 2>, <o, 3>]
- 应用规则,根据规则,判断每个待测字符串是否满足标准。其实就是根据字母的次数决定字母在待测字符串的正确存在。具体如下:
- 出现次数小于3:根据题意可知,这组字母不属于扩展组,所以待测字符串中必须有等于该次数的该字母。
- 出现次数大于等于3:属于扩展组,待测字符串中可以有小于等于该次数的该字母。
- 示例代码
class Solution {
public:
int expressiveWords(string S, vector<string>& words) {
int size = S.length();
char before = '\0';
int counter = ;
vector<pair<int, int>> rules;
for(int i = ; i < size; i++)
{
if(S[i] == before)
{
counter += ;
}
else
{
// update rules
if(counter > )
rules.push_back(make_pair(before, counter));
// restart counter
before = S[i];
counter = ;
}
}
// update rules
if(counter > )
rules.push_back(make_pair(before, counter));
int ruleSize = rules.size();
int result = ;
size = words.size();
for(int i = ; i < size; i++)
{
// if equal
if(words[i] == S)
{
result += ;
continue;
}
// validate each word against rules
int wordSize = words[i].length();
bool isOk = true;
int currIndex = ;
for(int j = ; j < ruleSize; j++)
{
char curr = rules[j].first;
counter = rules[j].second;
int currCounter = ;
// counter this character in words vector
while(currIndex < wordSize && words[i][currIndex] == curr)
{
currCounter += ;
currIndex += ;
}
// non-extended(min)
if(counter < )
{
if(currCounter != counter)
{
isOk = false;
break;
}
}
// extended
else
{
if(currCounter > counter)
{
isOk = false;
break;
}
}
}
if(isOk)
{
result += ;
}
}
return result;
}
};
leetcode笔记(五)809. Expressive Words的更多相关文章
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
随机推荐
- Django组件——分页器(paginator)
一.视图层 from django.shortcuts import render # Create your views here. from .models import Book from dj ...
- POJ1659 Frogs' Neighborhood(Havel–Hakimi定理)
题意 题目链接 \(T\)组数据,给出\(n\)个点的度数,问是否可以构造出一个简单图 Sol Havel–Hakimi定理: 给定一串有限多个非负整数组成的序列,是否存在一个简单图使得其度数列恰为这 ...
- 什么时候修改class
点击按钮class发生改变 <html> <head> <meta http-equiv="Content-Type" content="t ...
- JS自定义手机端H5键盘
在输入车牌号的时候,因为很多车牌号都是数字字母混合排列的,所以如果用输入法输入就需要频繁切换数字跟字母,有点麻烦. 在这里我们就用自定义一个弹出框代替键盘来使用. 1.首先,要禁止掉文本框弹出输入法, ...
- javascript typeof()的用法与运算符用法
typeof 运算符 返回一个用来表示表达式的数据类型的字符串. typeof[()expression[]] ; expression 参数是需要查找类型信息的任意表达式. 说明 typeof 运算 ...
- WIN7系统程序放在中文文件夹打开报错及界面汉字变乱码
今天发现在一个服务商提供的设备的WIN7系统里,一个稳定运行的程序打开时报错,且界面汉字变乱码. 经测试发现程序放在英文名称的文件夹中可以正常打开,但界面上的汉字仍为乱码. 后检查“控制面板“--”区 ...
- C++ 数据库 char 转 wchar_t SQLWCHAR
C++中对数据库的操作感觉太复杂了,不如C#好使,但最近出于某些原因还是学习了一下C++下操作数据库的方法. 如果要想用C++实现对数据库的操作其实很简单,但是如果你需要动态的操作数据库(比如获得用户 ...
- Cloud Foundry Session Affinity(Sticky Session)的实现
会话保持(Session Affinity),有时又称粘滞会话(Sticky Sessions), 是负载均衡领域设计需要着力解决的重要问题之一,也是一个相对比较复杂的问题. 会话保持是指在负载均衡器 ...
- 一个查看UI5控件所有公有方法的小技巧
一个很小的tip:比如我想把UI5表格控件里的每列设置成宽度根据显示的内容自适应,需要知道应该调用控件的哪个方法来实现. 一种办法当然是查SAP帮助文档,得知需要调用控件的公有方法setAutoSiz ...
- 如何查找Fiori UI上某个字段对应的后台存储表的名称
今天微信群里有朋友问到这个问题. 如果是SAPGUI里的事务码,比如MM01,对于开发者来说这个任务非常容易完成. 比如我想知道下图"Sales Unit"这个字段的值到底保存在哪 ...