ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 809. Expressive Words_Medium tag: array, two pointers

2021-08-12 01:32:43  阅读:204  来源: 互联网

标签:heeellooo Medium word pointers two start words group hello


Sometimes people repeat letters to represent extra feeling. For example:

  • "hello" -> "heeellooo"
  • "hi" -> "hiiii"

In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h""eee""ll""ooo".

You are given a string s and an array of query strings words. A query word is stretchy if it can be made to be equal to s by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is three or more.

  • For example, starting with "hello", we could do an extension on the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has a size less than three. Also, we could do another extension like "ll" -> "lllll" to get "helllllooo". If s = "helllllooo", then the query word "hello" would be stretchy because of these two extension operations: query = "hello" -> "hellooo" -> "helllllooo" = s.

Return the number of query strings that are stretchy.

 

Example 1:

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 size 3 or more.

Example 2:

Input: s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"]
Output: 3

 

Constraints:

  • 1 <= s.length, words.length <= 100
  • 1 <= words[i].length <= 100
  • s and words[i] consist of lowercase letters.

Ideas:

1. helper function, check s and word 

2. 得到word[start_word]和s[start_s], 如果相等,再看他们的次数是否符合题意

3. 最后得到sum 并返回

 

Code:

class Solution:
    def expressiveWords(self, s: str, words: List[str]) -> int:
        return sum([self.exWord(s, 0, word, 0) for word in words])
    
    
    def exWord(self, s, start_s,  word, start_word) -> bool:
        l_s, l_word = len(s), len(word)
        if start_s == l_s and start_word == l_word: return True
        elif start_s >= l_s or start_word >= l_word:
            return False
        char = s[start_s]
        if char == word[start_word]:
            count1, count2 = 0, 0
            while start_s < l_s and s[start_s] == char:
                count1 += 1
                start_s += 1
            while start_word < l_word and word[start_word] == char:
                count2 += 1
                start_word += 1
            if count1 == count2 or (count1 >= 3 and count1 > count2):
                return self.exWord(s, start_s, word, start_word)
        return False
        
        

 

标签:heeellooo,Medium,word,pointers,two,start,words,group,hello
来源: https://www.cnblogs.com/Johnsonxiong/p/15130907.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有