【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 日 ———— 实验室椅子质量不行啊。。往后一仰,靠背断了。。

【LeetCode】809. Expressive Words 解题报告(Python)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. 46.Valid Parentheses

    Valid Parentheses My Submissions QuestionEditorial Solution Total Accepted: 106346 Total Submissions ...

  2. 32-3Sum

    相似题目 4sum http://blog.csdn.net/justdoithai/article/details/51195124 http://blog.csdn.net/justdoithai ...

  3. 日常Java 2021/10/31

    泛型类 泛型类的声明和非泛型类的声明类似,除了在类名后面添加了类型参数声明部分.和迈型方法一样,泛型类的类型参数声明部分也包含一个或多个类型参数,参数间用逗号隔开.一个泛型参数,也被称为一个类型变量, ...

  4. 《手把手教你》系列技巧篇(四十八)-java+ selenium自动化测试-判断元素是否可操作(详解教程)

    1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...

  5. MVC、MVVM模式

    MVC 上个世纪70年代,美国施乐帕克研究中心,就是那个发明图形用户界面(GUI)的公司,开发了Smalltalk编程语言,并开始用它编写图形界面的应用程序. 到了Smalltalk-80这个版本的时 ...

  6. 【STM32】WS2812介绍、使用SPI+DMA发送数据

    这篇要使用到SPI+DMA,需要了解的话,可以参考我另两篇博客 时钟:https://www.cnblogs.com/PureHeart/p/11330967.html SPI+DMA通信:https ...

  7. recyclerView DiffUtil使用

    DiffUtil是和RecyclerView一块用的,DiffUtil用来比较两个数据集,他的最大用处是在RecyclerView刷新时,不在无脑. 以前adapter.notifyDataSetCh ...

  8. Activity 详解

    1.活动的生命周期 1.1.返回栈 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈是一种先进后出的数据结构,在默认情况下,每当我们启 ...

  9. 【Java 基础】java 创建对象时重写方法

    TransactionLock mockLock = new TransactionLock() { public boolean lock(String id) { return true; } p ...

  10. springmvc中拦截器的定义和配置

    package com.hope.interceptor;import org.springframework.lang.Nullable;import org.springframework.web ...