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

复制代码
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


    }

}
相关推荐
张彦峰ZYF1 分钟前
投资策略规划最优决策分析
分布式·算法·金融
以后不吃煲仔饭10 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师11 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
The_Ticker16 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
Json_1817901448033 分钟前
电商拍立淘按图搜索API接口系列,文档说明参考
前端·数据库
大数据编程之光39 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
爪哇学长1 小时前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法
风尚云网1 小时前
风尚云网前端学习:一个简易前端新手友好的HTML5页面布局与样式设计
前端·css·学习·html·html5·风尚云网
Dola_Pan1 小时前
C语言:数组转换指针的时机
c语言·开发语言·算法
ExiFengs1 小时前
实际项目Java1.8流处理, Optional常见用法
java·开发语言·spring