Java “收缩字符串”;abbccbfgh“;通过删除连续的k个字符,直到无法删除为止

Java “收缩字符串”;abbccbfgh“;通过删除连续的k个字符,直到无法删除为止,java,logic,Java,Logic,通过删除连续的k个字符来收缩字符串“abbccbfgh”,直到无法删除为止。 e、 g.对于k=3,上述字符串的输出将为“afgh”。 请注意,K和string都是动态的,即由用户提供 我写了下面的程序,但无法完成。请帮忙 public class Test { public static void main(String[] args) { String str = "abbcccbfgh"; int k = 3; String re

通过删除连续的k个字符来收缩字符串“abbccbfgh”,直到无法删除为止。 e、 g.对于k=3,上述字符串的输出将为“afgh”。 请注意,K和string都是动态的,即由用户提供

我写了下面的程序,但无法完成。请帮忙

public class Test {

    public static void main(String[] args) {
        String str = "abbcccbfgh";
        int k = 3;

        String result = removeConsecutive(str, k);
        System.out.print("result is " + result);
    }

    private static String removeConsecutive(String str, int k) {
        String str1 = str + "";
        String res = "";
        int len = str.length();
        char c1 = 0, c2 = 0;
        int count = 0;
        for (int i = 0; i < len - 1; i++) {
            c1 = str.charAt(i);
            c2 = str.charAt(i + 1);

            if (c1 == c2) {
                count++;
            } else {

                res = res + String.valueOf(c1);
                count = 0;
            }
            if (count == k-1) {
                //remove String

            }
        }

        return res;
    }
公共类测试{
公共静态void main(字符串[]args){
String str=“abbccbfgh”;
int k=3;
字符串结果=removeConsecutive(str,k);
系统输出打印(“结果为”+结果);
}
私有静态字符串removeConsecutive(字符串str,int k){
字符串str1=str+“”;
字符串res=“”;
int len=str.length();
字符c1=0,c2=0;
整数计数=0;
对于(int i=0;i
我建议使用正则表达式:

    int l = 0;
    do {
        l = str.length();
        str = str.replaceAll("(.)\\1{" + n + "}", "");
    } while (l != str.length());
n=k-1


()\1{2}表示后跟n个相同字符的任何字符。\1表示与组#1中相同的字符可以进行递归吗

public class Test {

    public static void main(String[] args) {
        String str = "abbcccbfgh";
        int k = 3;

        String result = removeConsecutive(str, k);
        System.out.print("result is " + result);
    }

    private static String removeConsecutive(String str, int k) {

        String ret = str;
        int len = str.length();

        int count = 0;
        char c1 = 0 ;
        char c2 = 0;
        char last = 0 ;

        for (int i = 0; i < ret.length()-1; i++) {
            last = c1 ;
            c1 = str.charAt(i);
            c2 = str.charAt(i + 1);

            if (c1 == c2 ) {
                if( count > 0 ) {
                    if( last == c1 ) {
                        count ++ ;
                    }
                    else {
                        count = 0;
                    }
                }
                else {
                count++;
                }
            } else {
                count = 0;
            }


            if (count == k-1) {


                int start = ((i+1) - k) + 1 ;

                String one = str.substring(0, start) ;
                String two = str.substring(start+k);

                String new1 = one + two ;
                //recursion
                ret = removeConsecutive(new1, k) ;
                count = 0;

            }
        }

        return ret;
    }
}
公共类测试{
公共静态void main(字符串[]args){
String str=“abbccbfgh”;
int k=3;
字符串结果=removeConsecutive(str,k);
系统输出打印(“结果为”+结果);
}
私有静态字符串removeConsecutive(字符串str,int k){
字符串ret=str;
int len=str.length();
整数计数=0;
字符c1=0;
字符c2=0;
char last=0;
对于(int i=0;i0){
如果(最后==c1){
计数++;
}
否则{
计数=0;
}
}
否则{
计数++;
}
}否则{
计数=0;
}
如果(计数=k-1){
int start=((i+1)-k)+1;
字符串一=str.substring(0,开始);
字符串二=str.substring(start+k);
字符串new1=1+2;
//递归
ret=移除连续性(new1,k);
计数=0;
}
}
返回ret;
}
}

您可以使用堆栈来完成。对于字符串中的每个字符
ch
,将其推到堆栈中,如果有3个连续的相同字符,则将其全部弹出。最后,将堆栈转换为字符串。您可以使用一个特殊堆栈来稍微改进程序,该堆栈可以记住每个元素的出现次数

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class Reduce implements Function<String, String> {
    private final int k;

    public Reduce(final int k) {
        if (k <= 0) {
            throw new IllegalArgumentException();
        }
        this.k = k;
    }

    @Override
    public String apply(final String s) {
        Stack<Character> stack = new Stack<>();
        for (Character ch : s.toCharArray()) {
            stack.push(ch);
            if (stack.topCount() == k) {
                stack.pop();
            }
        }
        return stack.toString();
    }

    public static void main(String[] args) {
        Reduce reduce = new Reduce(3);
        System.out.println(reduce.apply("abbcccbfgh"));
    }

    private static class Stack<T> {
        private class Node {
            private T value;
            private int count;

            Node(T value) {
                this.value = value;
                this.count = 1;
            }
        }
        private List<Node> nodes = new ArrayList<>();

        public void push(T value) {
            if (nodes.isEmpty() || !top().value.equals(value)) {
                nodes.add(new Node(value));
            } else {
                top().count++;
            }
        }

        public int topCount() {
            return top().count;
        }

        public void pop() {
            nodes.remove(nodes.size()-1);
        }

        private Node top() {
            return nodes.get(nodes.size()-1);
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            nodes.forEach(n->{
                for (int i = 0; i < n.count; i++) {
                    sb.append(n.value);
                }
            });
            return sb.toString();
        }
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.function.function;
公共类Reduce实现函数{
私人终审法院;
公共减少(最终整数k){
if(k){
对于(int i=0;i
为什么“abbccbfgh”会导致“afgh”?为什么要将两个b一起删除,而不删除其他字符?首先,当您附加到字符串时,最好使用
StringBuilder
s,而不仅仅是
String
s,因为
String
s是不可变的。至于您的问题,请尝试使用存储字符及其当前计数的堆栈。我假设
b
c
被删除,因为字符串中字符的计数等于
k
?那么创建两个循环怎么样。第一个循环对字符串中出现的所有字符进行计数(将每个字符的计数保存在新数组中)。第二个循环返回的字符串的字符计数不等于
k
(使用我们之前创建的数组)。@daniu我相信在删除k=3个连续的Cs后,“abbccbfgh”将变为“abb bfgh”。这导致“abbbfgh”变成“afgh”。@Vj-有道理,我忘了应用“连续”规则。你能给我一个不使用正则表达式的方法吗