comments | difficulty | edit_url | rating | source | tags | ||
---|---|---|---|---|---|---|---|
true |
中等 |
1490 |
第 228 场周赛 Q2 |
|
给你一个字符串 s
,返回 s
中 同质子字符串 的数目。由于答案可能很大,只需返回对 109 + 7
取余 后的结果。
同质字符串 的定义为:如果一个字符串中的所有字符都相同,那么该字符串就是同质字符串。
子字符串 是字符串中的一个连续字符序列。
示例 1:
输入:s = "abbcccaa" 输出:13 解释:同质子字符串如下所列: "a" 出现 3 次。 "aa" 出现 1 次。 "b" 出现 2 次。 "bb" 出现 1 次。 "c" 出现 3 次。 "cc" 出现 2 次。 "ccc" 出现 1 次。 3 + 1 + 2 + 1 + 3 + 2 + 1 = 13
示例 2:
输入:s = "xy" 输出:2 解释:同质子字符串是 "x" 和 "y" 。
示例 3:
输入:s = "zzzzz" 输出:15
提示:
1 <= s.length <= 105
s
由小写字符串组成。
遍历字符串
遍历完字符串
时间复杂度
class Solution:
def countHomogenous(self, s: str) -> int:
mod = 10**9 + 7
i, n = 0, len(s)
ans = 0
while i < n:
j = i
while j < n and s[j] == s[i]:
j += 1
cnt = j - i
ans += (1 + cnt) * cnt // 2
ans %= mod
i = j
return ans
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int countHomogenous(String s) {
int n = s.length();
long ans = 0;
for (int i = 0, j = 0; i < n; i = j) {
j = i;
while (j < n && s.charAt(j) == s.charAt(i)) {
++j;
}
int cnt = j - i;
ans += (long) (1 + cnt) * cnt / 2;
ans %= MOD;
}
return (int) ans;
}
}
class Solution {
public:
const int mod = 1e9 + 7;
int countHomogenous(string s) {
int n = s.size();
long ans = 0;
for (int i = 0, j = 0; i < n; i = j) {
j = i;
while (j < n && s[j] == s[i]) ++j;
int cnt = j - i;
ans += 1ll * (1 + cnt) * cnt / 2;
ans %= mod;
}
return ans;
}
};
func countHomogenous(s string) (ans int) {
n := len(s)
const mod int = 1e9 + 7
for i, j := 0, 0; i < n; i = j {
j = i
for j < n && s[j] == s[i] {
j++
}
cnt := j - i
ans += (1 + cnt) * cnt / 2
ans %= mod
}
return
}
function countHomogenous(s: string): number {
const mod = 1e9 + 7;
const n = s.length;
let ans = 0;
for (let i = 0, j = 0; j < n; j++) {
if (s[i] !== s[j]) {
i = j;
}
ans = (ans + j - i + 1) % mod;
}
return ans;
}
impl Solution {
pub fn count_homogenous(s: String) -> i32 {
const MOD: usize = (1e9 as usize) + 7;
let s = s.as_bytes();
let n = s.len();
let mut ans = 0;
let mut i = 0;
for j in 0..n {
if s[i] != s[j] {
i = j;
}
ans = (ans + j - i + 1) % MOD;
}
ans as i32
}
}
public class Solution {
public int CountHomogenous(string s) {
long MOD = 1000000007;
long ans = 0;
for (int i = 0, j = 0; i < s.Length; i = j) {
j = i;
while (j < s.Length && s[j] == s[i]) {
++j;
}
int cnt = j - i;
ans += (long) (1 + cnt) * cnt / 2;
ans %= MOD;
}
return (int) ans;
}
}
int countHomogenous(char* s) {
int MOD = 1e9 + 7;
int ans = 0;
for (int i = 0, j = 0; s[j]; j++) {
if (s[i] != s[j]) {
i = j;
}
ans = (ans + j - i + 1) % MOD;
}
return ans;
}
class Solution:
def countHomogenous(self, s: str) -> int:
mod = 10**9 + 7
ans = cnt = 1
for a, b in pairwise(s):
cnt = cnt + 1 if a == b else 1
ans = (ans + cnt) % mod
return ans
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int countHomogenous(String s) {
int n = s.length();
int ans = 1, cnt = 1;
for (int i = 1; i < n; ++i) {
cnt = s.charAt(i) == s.charAt(i - 1) ? cnt + 1 : 1;
ans = (ans + cnt) % MOD;
}
return ans;
}
}
class Solution {
public:
const int mod = 1e9 + 7;
int countHomogenous(string s) {
int n = s.size();
int ans = 1, cnt = 1;
for (int i = 1; i < n; ++i) {
cnt = s[i] == s[i - 1] ? cnt + 1 : 1;
ans = (ans + cnt) % mod;
}
return ans;
}
};
func countHomogenous(s string) int {
n := len(s)
const mod int = 1e9 + 7
ans, cnt := 1, 1
for i := 1; i < n; i++ {
if s[i] == s[i-1] {
cnt++
} else {
cnt = 1
}
ans = (ans + cnt) % mod
}
return ans
}