Weekly Contest 78-------->809. Expressive Words (stack)
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
.S
and all words inwords
consist only of lowercase letters
Approach #1: C++.
class Solution {
public:
int expressiveWords(string S, vector<string>& words) {
int ans = 0;
stack<char> stk1;
for (int i = 0; i < S.length(); ++i)
stk1.push(S[i]);
for (int i = 0; i < words.size(); ++i) {
stack<char> stk2;
for (int j = 0; j < words[i].length(); ++j)
stk2.push(words[i][j]);
ans += helper(stk1, stk2);
}
return ans;
} private:
int helper(stack<char> stk1, stack<char> stk2) {
int t1, t2;
if (stk1.size() < stk2.size()) return 0;
while (!stk1.empty() && !stk2.empty()) {
t1 = 0, t2 = 0;
char c1 = stk1.top();
char c2 = stk2.top();
stk1.pop(), stk2.pop();
if (c1 != c2) return 0;
while (!stk1.empty() && stk1.top() == c2) {
t1++;
stk1.pop();
}
while (!stk2.empty() && stk2.top() == c1) {
t2++;
stk2.pop();
}
if (t1 == t2 || t1 >= 2) continue;
else return 0;
}
if (stk2.empty())
return 1;
}
};
By this problem I learned the importance of appropriate data structure.
Weekly Contest 78-------->809. Expressive Words (stack)的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 112th LeetCode Weekly Contest Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 101
前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不 ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
随机推荐
- linux shell 的前世今生和流行BASH SHELL的特点
前言 shell作为用户和操作系统内核交互的接口,也不断的在发展迭代.shell的发展也离不开unix/linux 系统的发展.并且在开源社区对shell的发展也起到了推动作用. 内容思维导图简介 发 ...
- Eclipse内存错误java heap space
Eclipse安装路径下的内存配置文件:eclipse.ini 文件末尾: -XX:MaxPermSize=256m-Xms40m-Xmx512m 其中 -Xmx512m表示最大内存,改为768或10 ...
- HBase开发
MapReduce接口 HBase提供了TableInputFormat.TableOutputFormat.TableMapper和TableReducer类来支持使用MapReduce框架处理HB ...
- h5 移动端 关于监测切换程序到后台或息屏事件和visibilitychange的使用
需求:当我们页面上正在播放视频或者播放背景音乐时,我们屏幕自动息屏或者切换程序去看消息时,我们希望暂停视频或背景音乐,回到程序我们希望继续播放视频或播放背景音乐.小程序上提供了 onUnload返回 ...
- [noi2002]M号机器人
3030年,Macsy正在火星部署一批机器人.第1秒,他把机器人1号运到了火星,机器人1号可以制造其他的机器人.第2秒,机器人1号造出了第一个机器人——机器人2号.第3秒,机器人1号造出了另一个机器人 ...
- HTML初级教程
1:标题h1~h6HTML标签有专门的标签处理你页面上的标题,它们是h1,h2,h3,h4,h5和h6,象封建社会一样,h1就是万能的君主而h6就是最底层的百姓. 注意,h1标签在一个页面只能使用一次 ...
- sqlserver锁表查看
sp_lock--查询哪个进程锁表了,spid:进程ID,ObjId:对象ID EXEC sp_executesql N'KILL [spid]'--杀进程 select object_name([O ...
- oracle添加表注释和表字段注释
创建Oracle数据库表时加上注释 CREATE TABLE t1( id varchar2(32) primary key,name VARCHAR2(8) NOT NULL, age numbe ...
- 记录一个读pcap数据包的软件:Fiddler
Fiddler.大神推荐的.名字老忘. 用wireshark在wifi共享精灵共享出来的无线网上抓包,发现一个SSDP(简单服务发现协议)一直在尝试找连上这个网络上的设备. 连上NEXUS4后出现了I ...
- BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP
BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP Description In an effort to better manage t ...