• 题目描述

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:

  1. 0 <= len(S) <= 100.
  2. 0 <= len(words) <= 100.
  3. 0 <= len(words[i]) <= 100.
  4. S and all words in words consist only of lowercase letters
  • 解题思路

题目描述挺多~题目属于第一眼简单题,知道属于字符串匹配,知道最好用正则表达式。但是无奈记不清C++ regex类的用法

所以,就退而求其次,用最笨的思路实现下字符串匹配。主要思路:

  1. 建立规则,根据标准字符串S,计算字符串匹配的规则,表示为字母和字母次数的pair。如

    S = "heeellooo" --> [<h, 1>, <e, 3>, <l, 2>, <o, 3>]
  2. 应用规则,根据规则,判断每个待测字符串是否满足标准。其实就是根据字母的次数决定字母在待测字符串的正确存在。具体如下:
    • 出现次数小于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的更多相关文章

  1. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

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

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  5. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  6. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  8. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  9. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  10. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

随机推荐

  1. Java 的版本历史与特性

    Java SE 8[2014-03-14发行] Lambda表达式 Pipelines和Streams Date和Time API Default方法 Type注解 Nashhorn JavaScri ...

  2. CSS 面包屑导航栏

    做之前,先看一下效果图. demo01.png 首先,书写好 HTML 代码. <div id="crumbs"> <ul> <li><a ...

  3. Jupyter Notebook(iPython)

    一.Jupyter Notebook介绍 1.什么是Jupyter Notebook Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过编码开发.文档编写.运行代码和 ...

  4. Android 获取系统时间和网络时间

    有些时候我们的应用中只能使用网络时间,而不能使用系统的时间,这是为了避免用户关闭了使用网络时间的功能后所产生的误差. 直接上代码. 1.清单文件中网络添加权限. <!-- 访问Internet资 ...

  5. 爬楼梯C++

    class Solution {public: /** * @param n: An integer * @return: An integer */ int climbStairs(int n) { ...

  6. java面试题之----String的intern

    When---什么时候需要了解String的intern方法: 面试的时候(蜜汁尴尬)!虽然不想承认,不过面试的时候经常碰到这种高逼格的问题来考察我们是否真正理解了String的不可变性.String ...

  7. 【Spring实战】—— 1 入门讲解

    这个系列是学习spring实战的总结,一方面总结书中所写的精髓,另一方面总结一下自己的感想. 基础部分讲解了spring最为熟知的几个功能:依赖注入/控制反转 和 面向切面编程. 这两个就不再多说了, ...

  8. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

  9. Ubuntu 下安装Mongodb

    Mongodb是一款开源的数据库,这里不用我多说了,下面说一下Ubuntu下安装Mongodb可能遇到的问题和解决方案. 故事背景: 今天M$促销,1¥Windows Azure 4000¥-30天的 ...

  10. 长大Tips的第二步

    由于期末将至的缘故,组员们对于这次项目都开始表现出了懈怠的情绪,故而这一次并没有完成许多实质性的任务,相较于上一次,此次增添了登陆以及注册的功能,说来惭愧,虽然已经学习了数据库编程,可惜自己学艺不精并 ...