🔗 leetcode.com/problems/fi…
题目
- 给一个全小写字符串 s
- special 字符串的定义为,该字符串的所有字符相同,比如
"ddd", "zz", "f"
- 返回 s 中至少出现三次的 special 字符串的最大长度
- 若不存在返回 -1
思路
- 用数组 vec[’a’ - ‘z’][len] 统计 ‘a’ - ’z’ 长度 len 的 special 字符串出现的频次
- 最后从大到小遍历 len,若出现频次大于 3,则是 special 字符串的最大长度
代码
class Solution {
public:
int maximumLength(string s) {
int ans = 0
int vec[26][55]
memset(vec, 0, sizeof vec)
vec[s[0] - 'a'][1] = 1
int freq = 1
int len = 1
for (int i = 1
if (s[i] == s[i-1]) {
freq++
for (int j = freq
vec[s[i] - 'a'][j]++
len = max(len, freq)
} else {
freq = 1
vec[s[i] - 'a'][freq]++
}
}
for (int i = len
for (int j = 0
if (vec[j][i] >= 3) return i
}
}
return -1
}
}