超过三个连续重复的字母删除

复制代码
package com.ag;

public class Test {
    /**
     * For a given string that only contains alphabet characters a-z, if 3 or more consecutive
     * characters are identical, remove them from the string. Repeat this process until
     * there is no more than 3 identical characters sitting besides each other.
     * Example:
     * Input: aabcccbbad
     * Output:
     * -> aabbbad
     * -> aaad
     * -> d
     */
    public String removeConsecutive(String s) {
        StringBuilder result = new StringBuilder();
        if (s.length() >= 1) {
            for (int i = 0; i < s.length(); i++) {
                char currentChar = s.charAt(i);
                int count = 0;

                for (int j = 0; j < s.length(); j++) {
                    if (s.charAt(j) == currentChar) {
                        count++;
                    }
                }
                if (count <= 2) {
                    result = result.append(currentChar);
                }
            }
        }
        return result.toString();
    }


    /**
     *#Stage 2 - advanced requirement
     * Instead of removing the consecutively identical characters, replace them with a
     * single character that comes before it alphabetically.
     * Example:
     * ccc -> b
     * bbb -> a
     * Input: abcccbad
     * Output:
     * -> abbbad, ccc is replaced by b
     * -> aaad, bbb is replaced by a
     * -> d
     */
    public String replaceConsecutive(String s) {
        while (true) {
            StringBuilder newString = new StringBuilder();
            int count = 1;

            for (int i = 1; i < s.length(); i++) {
                if (i > 0 && s.charAt(i) == s.charAt(i - 1)) {
                    count++;
                } else {
                    if (count >= 3) {
                        // 替换为前一个字母
                        if ((s.charAt(i - 1) - 1) == 96) {
                            newString.append("");
                        } else {
                            char newChar = (char) (s.charAt(i - 1) - 1);
                            newString.append(newChar);
                        }
                    } else {
                        for (int j = 0; j < count; j++) {
                            newString.append(s.charAt(j));
                        }
                    }
                    count = 1;
                }
            }

            // 处理最后一组字符
            if (count >= 3) {
                char newChar = (char) (s.charAt(s.length() - 1) - 1);
                newString.append(newChar);
            } else {
                for (int j = 0; j < count; j++) {
                    newString.append(s.charAt(s.length() - 1));
                }
            }

            String newStr = newString.toString();
            if (newStr.equals(s)) { // 没有变化
                break;
            }
            s = newStr;
        }
        return s;
    }


    public static void main(String[] args) {
        Test t = new Test();
        String s1 = "aabcccbbad";
        String s2 = "abcccbad";
        System.out.println(t.removeConsecutive(s1));   // d
        System.out.println(t.replaceConsecutive(s2));  // d


    }

}
相关推荐
一条泥憨鱼2 分钟前
【Redis | 黑马点评】基于Redis的消息队列 | stream消息队列 源码解读
java·数据库·redis·缓存·lua
aibo8603086 分钟前
基于 Spring Boot + Vue3 的博物馆藏品管理系统设计与实现
java·spring boot·后端
fanruitian10 分钟前
python 进程 线程
java·前端·python
renhongxia113 分钟前
RAG检索增强生成:打通大模型与外部知识的通道
大数据·人工智能·算法
\光辉岁月/13 分钟前
javaSE8-新特性-Stream&Option
java·stream·option
ZoeLandia15 分钟前
React03:useState 与不可变更新
前端·react.js·typescript·react
zzu123zsw17 分钟前
把条件校验收敛到枚举接口:Spring Boot 统一断言模式与零污染异常切面工程化
java·spring boot·后端·架构设计
SL_staff20 分钟前
计划系统落地难?从开发者视角重构执行入口体验
java·开源·全栈
2501_9339232525 分钟前
双指针算法:移动零+复写零(Java)
数据结构·算法·双指针
蒲公英内测分发31 分钟前
智能手表卖出去后,包装、说明书和官网里的 App 下载入口怎么统一?
前端·人工智能·测试工具·智能家居·智能硬件·智能手表