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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/expressive-words/description/

题目描述:

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

题目大意

给出了一个字符串,其中有些字母是为了表现“情绪”而重复出现了多次,给出了一个列表,看列表中有多少个可以是这个字符串的源字符串。前提:表现语气最少需要一个字母重复三次。

解题方法

出题人真会玩啊,这个题首先把源字符串做分割,把列表中的每个词也做分割,判断源字符串的分割能否被列表中单词的分割一一对应上。其实重点就是如何按照重复情况进行字符串分割。

另外,判断能否被表达的方式是,分割出来的元素个数是一致的,如果S的分割的字符长度小于3需要完全相等,否则需要大于word的长度。

class Solution(object):
def expressiveWords(self, S, words):
"""
:type S: str
:type words: List[str]
:rtype: int
"""
ans = 0
set_S = set(S)
S_list = []
pre_s, pre_index = S[0], 0
for i, s in enumerate(S):
if pre_s != s:
S_list.append(S[pre_index:i])
pre_s, pre_index = s, i
if i == len(S) - 1:
S_list.append(S[pre_index:])
for word in words:
if set(word) != set_S:
continue
word_list = []
pre_w, pre_index = word[0], 0
for i, w in enumerate(word):
if pre_w != w:
word_list.append(word[pre_index:i])
pre_w, pre_index = w, i
if i == len(word) - 1:
word_list.append(word[pre_index:])
if len(S_list) == len(word_list):
if all(S_list[i] == word_list[i] if len(S_list[i]) < 3 else len(S_list[i]) >= len(word_list[i]) for i in range(len(S_list))):
ans += 1
return ans

日期

2018 年 6 月 13 日 ———— 实验室椅子质量不行啊。。往后一仰,靠背断了。。

最新文章

  1. AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决
  2. button
  3. 配置指定使用tcc编译器编译nim程序
  4. MongoDB系列一(索引及C#如何操作MongoDB)
  5. C8051 PCA实现红外遥控接收
  6. PyQt 5.2 发布,此版本完全支持Qtv5.2.0
  7. NGINX(三)HASH表
  8. verilog中=和&lt;=的区别
  9. 解决Unity3d 4.3 动画系统带来的烦恼
  10. python 中如何导入一个自己创建的模块
  11. xml读取一行数据
  12. Eggjs 设置跨域请求
  13. poj1017----模拟
  14. 知识蒸馏(Distillation)
  15. freeswitch编译java esl
  16. Redis数据结构:SDS
  17. 【HDU2138】How many prime numbers
  18. 系统常量对话框QT实现
  19. JNLP Slave connection error解决办法
  20. python搜索引擎(转)

热门文章

  1. Shell 格式化输出printf、awk
  2. InnoDB学习(二)之ChangeBuffer
  3. Spark(三)【RDD中的自定义排序】
  4. 转 Android Studio中Junit调试
  5. ListView的item不能点击(焦点冲突问题)
  6. jenkins+Gitlab安装及初步使用
  7. Does compiler create default constructor when we write our own?
  8. Servlet(1):Servlet介绍
  9. 关系型数据库和非关系型数据库区别、oracle与mysql的区别
  10. 【Matlab】快速傅里叶变换/ FFT/ fftshift/ fftshift(fft(fftshift(s)))