首页 > 其他分享 >[LeetCode] 1759. Count Number of Homogenous Substrings

[LeetCode] 1759. Count Number of Homogenous Substrings

时间:2022-12-26 09:35:24浏览次数:63  
标签:Count count 同构 Homogenous Number int homogenous 字符串 appears

Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 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.

Example 2:

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

Example 3:

Input: s = "zzzzz"
Output: 15

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase letters.

统计同构子字符串的数目。

给你一个字符串 s ,返回 s 中 同构子字符串 的数目。由于答案可能很大,只需返回对 109 + 7 取余 后的结果。

同构字符串 的定义为:如果一个字符串中的所有字符都相同,那么该字符串就是同构字符串。

子字符串 是字符串中的一个连续字符序列。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-number-of-homogenous-substrings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道数学题。思路是扫描 input 字符串,当遇到连续出现的字符串的时候,统计连续出现的次数 count,同构子字符串的数目 = (1 + count) * count / 2,就是小学数学里的等差数列求和。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public int countHomogenous(String s) {
 3         final int MOD = (int) Math.pow(10, 9) + 7;
 4         long res = 0;
 5         char pre = s.charAt(0);
 6         int count = 0;
 7         for (int i = 0; i < s.length(); i++) {
 8             char cur = s.charAt(i);
 9             if (cur == pre) {
10                 count++;
11             } else {
12                 res += (long) (count + 1) * count / 2;
13                 count = 1;
14                 pre = cur;
15             }
16         }
17         res += (long) (count + 1) * count / 2;
18         return (int) (res % MOD);
19     }
20 }

 

LeetCode 题目总结

标签:Count,count,同构,Homogenous,Number,int,homogenous,字符串,appears
From: https://www.cnblogs.com/cnoodle/p/17004985.html

相关文章

  • POJ--2386 Lake Counting(DFS)
    记录23:182022-12-25http://poj.org/problem?id=2386reference:《挑战程序设计竞赛(第2版)》2.1.4p33DescriptionDuetorecentrains,waterhaspooledinvariou......
  • Powerful Number 筛
    PN筛。有时候确实比Min_25筛快。首先定义PowerfulNumber为形如\(a^2b^3\)的数。一个结论是\(n\)以内的PowerfulNumber是\(O(\sqrtn)\)的。证明枚举\(a\)......
  • Yii 分组统计写法(group by count)
    最终写出的查询语句://近30天的销量privatefunctionlast30DaySale($storeIdArr){$time_30day=strtotime('-30days');$list=......
  • Chapter 1 Why Biology by the Number
    1.生物物理学和模型构建生物物理学是应用物理学的概念和方法研究生物各个层次结构与功能的关系,生命活动的物理,物理化学过程和物质在生命活动过程中表现的物理特性的学科,......
  • docker安装elasticsearch时max virtual memory areas vm.max_count(65530) is too low
    利用docker-compose安装elasticsearch时启动失败的异常解决maxvirtualmemoryareasvm.max_count(65530)istoolow...一.异常现象我在利用docker-compose进行elasticse......
  • js中Number数字类型没有length属性
    Number类型是没有length属性的,可以参考MDN文档Number类型的描述延伸问题:我在浏览器控制台里直接输入78.length回车是报错的,但是,varsomeValue=78;varstrLength=......
  • [CF1748E] Yet Another Array Counting Problem
    题目描述Thepositionoftheleftmostmaximumonthesegment$[l;r]$ofarray$x=[x_1,x_2,\ldots,x_n]$isthesmallestinteger$i$suchthat$l\l......
  • CountDownLatch简单使用
    如何保证主线程在副线程执行结束后才会执行结束,这里使用CountDownLatch    //设置三个线程需要执行CountDownLatchlatch=newCountDownLatch(3);......
  • 【PTA】1049 Counting Ones
    Thetaskissimple:givenanypositiveintegerN,youaresupposedtocountthetotalnumberof1'sinthedecimalformoftheintegersfrom1toN.Forexampl......
  • [CF1325E] Ehab's REAL Number Theory Problem
    Ehab'sREALNumberTheoryProblem题目描述Youaregivenanarray$a$oflength$n$thathasaspecialcondition:everyelementinthisarrayhasatmost7......