• 题目描述

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. 移除script标签引起的兼容性问题

    一.应用场景: 有时候我们需要动态创建script标签实现脚本的按需加载,我们会为script标签绑定onload或者onreadystatechange事件,用于检测动态脚本是否加载并执行完毕,在事 ...

  2. Dubbo与Zookeeper、Spring整合使用 maven+springmvc+dubbo+zookeeper

    为什么要用dubbo?   还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm   http://dubbo.io/   一般 nginx+tomcat  | - ...

  3. 类的方法练习——定义MySQL类

    要求: 1.对象有id.host.port三个属性 2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一 3.提供两种实例化方式,方式一:用户传入host和port 方式二: ...

  4. 基于Ajax与用户认证系统的登录验证

    一.登录页面 from django.contrib import admin from django.urls import path from blog import views urlpatte ...

  5. linux环境下安装jdk(本文示例是jdk1.6.0_45)

    第一步:创建一个文件夹安装jdk(虽说地址一般自定义,但是为了方便查找请按照笔者建议目录 ):/usr/java 将jdk-6u45-linux-x64.bin文件放到   /usr/java 文件夹 ...

  6. js CheckBox 全选、反选

    <h3>你最喜欢的水果是?</h3> <label><input type="checkbox"/>苹果</label> ...

  7. JS中的继承方式总结

    1. 原型链继承(又称类继承) Child.prototype = new Parent(); function Parent (name, age) { this.name = name; this ...

  8. maven---Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4

    Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 Failure to transfer or ...

  9. event.cancelBubble=true

    <tr><a href="xxx">连接</a></tr> 如上结构,单击tr的时候跳转至另一页面 <tr style=&qu ...

  10. npm 安装axios和使用增删改查

    1:安装axios(建议安装淘宝镜像) npm install axios 2:项目导入 npm install --save axios vue-axios 3:页面导入 import axios ...