简单题,简单做(Python/Java/C++/C/Go/JS/Rust)
把 $s$ 按照连续相同字母分成若干段,每段保留至多 $2$ 个字母。
示例 2 的 $s=\texttt{aaabaaaa}$,分成三段 $\texttt{aaa} + \texttt{b} + \texttt{aaaa}$,其中第一段和第三段不符合要求(有三个连续相同字符),保留 $2$ 个字母,变成 $\texttt{aa} + \texttt{b} + \texttt{aa} = \texttt{aabaa}$。
用一个计数器 $\textit{cnt}$ 统计每一段的当前长度,如果 $\textit{cnt}<3$ 就把当前字母加入答案。
如果当前字母和下一个字母不同,则重置 $\textit{cnt}=0$,统计下一段的长度。
###py
class Solution:
def makeFancyString(self, s: str) -> str:
ans = []
cnt = 0
for i, ch in enumerate(s):
cnt += 1
if cnt < 3:
ans.append(ch)
if i < len(s) - 1 and ch != s[i + 1]:
cnt = 0 # 当前字母和下个字母不同,重置计数器
return ''.join(ans)
###py
class Solution:
def makeFancyString(self, s: str) -> str:
ans = []
for _, group in groupby(s):
ans += list(group)[:2]
return ''.join(ans)
###java
class Solution {
public String makeFancyString(String s) {
StringBuilder ans = new StringBuilder();
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
cnt++;
if (cnt < 3) {
ans.append(s.charAt(i));
}
if (i < s.length() - 1 && s.charAt(i) != s.charAt(i + 1)) {
cnt = 0; // 当前字母和下个字母不同,重置计数器
}
}
return ans.toString();
}
}
###cpp
class Solution {
public:
string makeFancyString(string s) {
string ans;
int cnt = 0;
for (int i = 0; i < s.size(); i++) {
cnt++;
if (cnt < 3) {
ans += s[i];
}
if (i < s.size() - 1 && s[i] != s[i + 1]) {
cnt = 0; // 当前字母和下个字母不同,重置计数器
}
}
return ans;
}
};
###c
char* makeFancyString(char* s) {
int cnt = 0, j = 0;
for (int i = 0; s[i]; i++) {
cnt++;
if (cnt < 3) {
s[j++] = s[i];
}
if (s[i] != s[i + 1]) {
cnt = 0; // 当前字母和下个字母不同,重置计数器
}
}
s[j] = '\0';
return s;
}
###go
func makeFancyString(s string) string {
ans := []byte{}
cnt := 0
for i, ch := range s {
cnt++
if cnt < 3 {
ans = append(ans, byte(ch))
}
if i < len(s)-1 && byte(ch) != s[i+1] {
cnt = 0 // 当前字母和下个字母不同,重置计数器
}
}
return string(ans)
}
###js
var makeFancyString = function(s) {
const ans = [];
let cnt = 0;
for (let i = 0; i < s.length; i++) {
cnt++;
if (cnt < 3) {
ans.push(s[i]);
}
if (i < s.length - 1 && s[i] !== s[i + 1]) {
cnt = 0; // 当前字母和下个字母不同,重置计数器
}
}
return ans.join('');
};
###rust
impl Solution {
pub fn make_fancy_string(s: String) -> String {
let s = s.into_bytes();
let mut ans = vec![];
let mut cnt = 0;
for (i, &ch) in s.iter().enumerate() {
cnt += 1;
if cnt < 3 {
ans.push(ch);
}
if i + 1 < s.len() && ch != s[i + 1] {
cnt = 0; // 当前字母和下个字母不同,重置计数器
}
}
unsafe { String::from_utf8_unchecked(ans) }
}
}
复杂度分析
- 时间复杂度:$\mathcal{O}(n)$,其中 $n$ 是 $s$ 的长度。
- 空间复杂度:$\mathcal{O}(n)$ 或 $\mathcal{O}(1)$,取决于实现。
分类题单
- 滑动窗口与双指针(定长/不定长/单序列/双序列/三指针/分组循环)
- 二分算法(二分答案/最小化最大值/最大化最小值/第K小)
- 单调栈(基础/矩形面积/贡献法/最小字典序)
- 网格图(DFS/BFS/综合应用)
- 位运算(基础/性质/拆位/试填/恒等式/思维)
- 图论算法(DFS/BFS/拓扑排序/基环树/最短路/最小生成树/网络流)
- 动态规划(入门/背包/划分/状态机/区间/状压/数位/数据结构优化/树形/博弈/概率期望)
- 常用数据结构(前缀和/差分/栈/队列/堆/字典树/并查集/树状数组/线段树)
- 数学算法(数论/组合/概率期望/博弈/计算几何/随机算法)
- 贪心与思维(基本贪心策略/反悔/区间/字典序/数学/思维/脑筋急转弯/构造)
- 链表、二叉树与回溯(前后指针/快慢指针/DFS/BFS/直径/LCA/一般树)
- 字符串(KMP/Z函数/Manacher/字符串哈希/AC自动机/后缀数组/子序列自动机)
欢迎关注 B站@灵茶山艾府