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

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


    }

}
相关推荐
麒麟而非淇淋12 分钟前
AJAX 入门 day1
前端·javascript·ajax
架构文摘JGWZ14 分钟前
Java 23 的12 个新特性!!
java·开发语言·学习
2401_8581205315 分钟前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
leon62515 分钟前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
CV工程师小林16 分钟前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
阿树梢19 分钟前
【Vue】VueRouter路由
前端·javascript·vue.js
Navigator_Z40 分钟前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表
Aic山鱼1 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
拾光师1 小时前
spring获取当前request
java·后端·spring
aPurpleBerry1 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j