码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode - 1759. Count Number of Homogenous Substrings


    Description

    Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 10^9 + 7.

    A string is homogenous if all the characters of the string are the same.

    A substring is a contiguous sequence of characters within a string.

    Example 1:

    Input: s = "abbcccaa"
    Output: 13
    Explanation: The homogenous substrings are listed as below:
    "a"   appears 3 times.
    "aa"  appears 1 time.
    "b"   appears 2 times.
    "bb"  appears 1 time.
    "c"   appears 3 times.
    "cc"  appears 2 times.
    "ccc" appears 1 time.
    3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Example 2:

    Input: s = "xy"
    Output: 2
    Explanation: The homogenous substrings are "x" and "y".
    
    • 1
    • 2
    • 3

    Example 3:

    Input: s = "zzzzz"
    Output: 15
    
    • 1
    • 2

    Constraints:

    1 <= s.length <= 10^5
    s consists of lowercase letters.
    
    • 1
    • 2

    Solution

    The most straightforward way is to count the frequency of each character in the string, then go through the string to count the answer.

    But for example 1, when we encounter a, we could add 1, and when we encounter bb, we could add 2(number of b) and 1(number of bb), similarly, for ccc we add 3(number of c), 2(number of cc) and 1(number of ccc).

    So instead of brute-force, we could go through the string, and compare the previous character and current character.

    Time complexity: o ( n ) o(n) o(n)
    Space complexity: o ( 1 ) o(1) o(1)

    Code

    class Solution:
        def countHomogenous(self, s: str) -> int:
            res = 0
            prev_c = None
            modulo = 1000000007
            for each_c in s:
                if each_c != prev_c:
                    if prev_c:
                        res += (1 + cnt) * cnt // 2
                        res %= modulo
                    prev_c = each_c
                    cnt = 0
                cnt += 1
            return (res + (1 + cnt) * cnt // 2) % modulo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    Unity粒子系统ParticleSystem各模块及其参数学习
    苹果官宣新品发布会 10月31日发布会与Mac有关
    hping3进行SYN Flood攻击
    边写代码边学习之mlflow
    哈喽GPT-4o——对GPT-4o 提示词的思考与看法
    视频号创作分成计划揭秘!300+收益轻松拿,幻术视频成爆款!
    小米AI实验室最新研究论文收录于COLING 2022,介绍一种更有效的鲁棒性神经机器翻译训练方法...
    【MySQL】数据类型——MySQL的数据类型分类、数值类型、小数类型、字符串类型
    linux--LVS负载均衡 DR模式 rr
    零基础学Python有什么建议?如何入门编程?
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/134323978
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号