• 题目描述

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. Json/Xml简介和处理模型

    JSON json简介 JSON是一种基于文本的数据交换格式,源自JavaScript,用于Web服务和其他连接的应用程序.以下部分介绍了JSON语法,JSON使用概述以及生成和解析JSON的最常用方 ...

  2. 类数组转数组Array.prototype.slice.call(arrayLike)

    转换方式:Array.prototype.slice.call(arrayLike) 附:(http://www.jianshu.com/p/f8466e83cef0) 首先Array.prototy ...

  3. 移动端点击a链接出现蓝色背景问题解决

    a:link, a:active, a:visited, a:hover { background: none; -webkit-tap-highlight-color: rgba(0,0,0,0); ...

  4. 使用HTML5 canvas做地图(2)瓦片以及如何计算的

    上一篇也说到瓦片,我们为什么使用瓦片?这一篇主要是关于如何拼接地图? 下面的一张图,可以一眼明了,地图是如何切割以及拼接的. 瓦片信息 瓦片信息包括切图原点,瓦片大小,格式,分辨率以及分辨率级别等. ...

  5. wxpython 简单表格控件

    import wx, wx.grid class GridData(wx.grid.PyGridTableBase): _cols = "a b c".split() _data ...

  6. C#操作CAD-调用winform

    个人认为用命令操作cad会比较便捷,但是鉴于好多人喜欢通过鼠标点击的方式操作cad,在此讲一下如何调用winform.前期准备请看上篇文章. 1.在新建好项目并引用接口dll的前提下,新建一个winf ...

  7. C# 生成CODE128条码

    using System; using System.Collections.Generic; using System.Data; using System.Drawing;    namespac ...

  8. python常用模块(一)

    #什么是模块呢?就是用一大坨代码来完成一个功能的代码集合,是不是简单易懂 #类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个 ...

  9. STM32-F429ZIT6-关于驱动安装

    第一步:下载驱动 1.个人百度云链接:http://pan.baidu.com/s/1dE8vxy5 密码:yow0 2.网站下载:这个还是直接百度吧. 第二步:驱动安装 注意:安装之前要先关闭安全监 ...

  10. Android——Intent,Bundle

    Intent——切换activity intent.setClass(first.this,second.class); startActivity(intent); Bundle——切换时数据传递 ...