Leetcode 3045. Count Prefix and Suffix Pairs II

这篇具有很好参考价值的文章主要介绍了Leetcode 3045. Count Prefix and Suffix Pairs II。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • Leetcode 3045. Count Prefix and Suffix Pairs II
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3045. Count Prefix and Suffix Pairs II

1. 解题思路

这一题的话思路上就是一个Trie树的思路来寻找前序字符,然后由于题目要求要同时满足前序和后序两个条件,因此找到每一个单词的前序子串之后再判断一下其是否同时为后序子串即可。

2. 代码实现

给出python代码实现如下:

class Trie:
    def __init__(self):
        self.trie = {}
        self.cnt = defaultdict(int)
    
    def add_word(self, word):
        trie = self.trie
        for c in word:
            trie = trie.setdefault(c, {})
        trie["eos"] = word
        self.cnt[word] += 1

    def find(self, word):
        ans = []
        trie = self.trie
        for c in word:
            if c not in trie:
                break
            trie = trie[c]
            if "eos" in trie:
                ans.append((trie["eos"], self.cnt[trie["eos"]]))
        return ans

class Solution:
    def countPrefixSuffixPairs(self, words: List[str]) -> int:
        trie = Trie()
        ans = 0
        for word in words:
            s = trie.find(word)
            for w, c in s:
                if word.endswith(w):
                    ans += c
            trie.add_word(word)
        return ans

提交代码评测得到:耗时805ms,占用内存115.4MB。文章来源地址https://www.toymoban.com/news/detail-833249.html

到了这里,关于Leetcode 3045. Count Prefix and Suffix Pairs II的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 2000. Reverse Prefix of Word

    Given a  0-indexed  string  word  and a character  ch ,  reverse  the segment of  word  that starts at index  0  and ends at the index of the  first occurrence  of  ch  ( inclusive ). If the character  ch  does not exist in  word , do nothing. For example, if  word = \\\"abcdefd\\\"  and  ch = \\\"d\\\" , then you should  reverse  the segment that

    2024年02月09日
    浏览(27)
  • No plugin found for prefix ‘install‘ in the current project and in the plugin groups 的解决方法

    【现象】 【解决方法】 settings.xml文件的mirrors,新增如下信息 新增后的截图如下:  

    2024年02月14日
    浏览(40)
  • Leetcode 357. Count Numbers with Unique Digits

    Given an integer n, return the count of all numbers with unique digits, x, where 0 = x 1 0 n 0 = x 10^n 0 = x 1 0 n . f(0) = 1 f(1) = 10 f(k) = 9 * 9 * 8 * … * (9 - k + 2)

    2024年02月19日
    浏览(32)
  • LeetCode2085. Count Common Words With One Occurrence

    Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays. Example 1: Input: words1 = [“leetcode”,“is”,“amazing”,“as”,“is”], words2 = [“amazing”,“leetcode”,“is”] Output: 2 Explanation: “leetcode” appears exactly once in each of the two arrays. We count this s

    2024年01月16日
    浏览(27)
  • LeetCode 2409. Count Days Spent Together【前缀和,容斥原理】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月17日
    浏览(27)
  • LeetCode每日一题——2520. Count the Digits That Divide a Number

    2520. Count the Digits That Divide a Number Given an integer num, return the number of digits in num that divide num. An integer val divides nums if nums % val == 0. Example 1: Input: num = 7 Output: 1 Explanation: 7 divides itself, hence the answer is 1. Example 2: Input: num = 121 Output: 2 Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twic

    2024年02月08日
    浏览(29)
  • LeetCode45.Jump-Game-II<跳跃游戏II>

    从上次大神那里获得的灵感  这题问的是次数,那么我们需要确保 1,能否跳到终点  2,得到次数. 第一次条获得的是nums[0],那么第一个数就是我们第一次能跳跃的范围.每次在范围里获得最大值.并且次数加一.然后进入下一次范围;即可得到次数;    

    2024年02月15日
    浏览(33)
  • LeetCode59 螺旋矩阵 II

    螺旋矩阵 II 循环不变量的应用 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。 示例 1: 输入:n = 3 输出:[[1,2,3],[8,9,4],[7,6,5]] 示例 2: 输入:n = 1 输出:[[1]] 提示: 1 = n = 20

    2024年01月23日
    浏览(29)
  • leetcode 45. 跳跃游戏 II

             本题为 跳跃游戏I 的升级版,保证可以到达终点的情况下,要求出最短的跳跃次数。         还是仿照 跳跃游戏I 的思路,定义一个cover用于记录最大覆盖范围,终止条件是:        cover = nums.size()-1   ,还要定义一个变量 largest 用于记录当前最远覆盖范围的下

    2024年02月14日
    浏览(32)
  • LeetCode 59. 螺旋矩阵 II

    题目链接:LeetCode 59. 螺旋矩阵 II 本题不涉及算法,只是简单的模拟,但是由于边界条件比较多,因此容易出错。 分析题干:题目要求按照右、下、左、上、这样的顺序对数组进行填充,填充的值为 1 ~ n*n ,因此问题的关键就是找到待填充的位置,将其值赋值为 i 即可。 由于

    2024年02月02日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包