用不允许出现的次数替换字符串中的所有连续重复项

我需要编写一个方法,它将一个字符串作为参数并返回一个新字符串,该字符串是通过用'n'该字符串的一个实例替换重复的相邻字母的每个实例而获得的。


例如,如果"aaabcccd"作为输入 String 和n =2,它返回"aabccd"。我已经尝试了以下代码,但没有得到预期的输出


String in = "aaadbbb";

char[] s = in.toCharArray();

int len = s.length;


int n = 2;

StringBuffer new_s = new StringBuffer("");

int count = 1;

char prev='\0';


for (int i = 0; i < len - 1; i++) {

    if (s[i] == s[i + 1]) {

       if(count <= n){

            new_s.append(s[i]);

           count++;

        }else{

         count=1;

       }

    } else {

        new_s.append(s[i]);

    }

}

   

System.out.println(new_s);

输出aaadb -预期-aadbb


呼如林
浏览 109回答 5
5回答

慕运维8079593

可以使用反向引用通过正则表达式魔术来完成。String in = "aaaaddbbbbc";int n = 2;String pattern = String.format("(([a-z])\\2{%d})\\2+", n - 1);System.out.println(in.replaceAll(pattern, "$1"));输出:ddbbc解释:里面的数字{}是n-1。([a-z])是一个捕获组,匹配从 a 到 z 的任何单个小写字母。由于它是表达式中的第二组括号,因此可以引用为2.(([a-z])\\2{n})意思是“匹配相同字母的 n+1 次重复”。它构成了第一个捕获组,我们将使用它作为替换\\2+匹配同一字母的所有额外重复。它们在更换后被丢弃。

largeQ

看看这个解决方案。您应该注意输入字符串中的最后一个字符,因为您只迭代到最后一个字符。private void replaceConsecutiveDuplicates() {&nbsp; &nbsp; String input = "aaadbbb";&nbsp; &nbsp; int n = 2;&nbsp; &nbsp; StringBuffer sb = new StringBuffer();&nbsp; &nbsp; int count = 1;&nbsp; &nbsp; char current;&nbsp; &nbsp; for( int i = 0; i < input.length(); ++i){&nbsp; &nbsp; &nbsp; &nbsp; current = input.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; if (i + 1 < input.length() && current == input.charAt(i + 1)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++count;&nbsp; &nbsp; &nbsp; &nbsp; } else if (count > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0; j < n; ++j) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(current);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(current);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sb.toString());}

www说

要再添加一个备选方案:&nbsp; &nbsp; String in = "aaadbbbjjkllllllopp";&nbsp; &nbsp; int n = 2;&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; char temp = in.charAt(0);&nbsp; &nbsp; for(int i = 0; i < in.length()-1;){&nbsp; &nbsp;// note that the incrementation of i is moved to the while loop&nbsp; &nbsp; &nbsp; &nbsp; temp = in.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // save current char in temp variable&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (i < in.length() && in.charAt(i) == temp) {&nbsp; &nbsp;///iterate as long as you find same chars or hit the end of the string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (count > n){&nbsp; &nbsp;// if and only if count is greater than max allowed set it to max allowed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = n;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0; j < count; j++){&nbsp; &nbsp;// append count chars&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(temp);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sb.toString());

摇曳的蔷薇

&nbsp; public static String test(String input, int repetitions) {&nbsp; &nbsp; String flag = "";&nbsp; &nbsp; String replacement = "";&nbsp; &nbsp; String output = input;&nbsp; &nbsp; ArrayList<Character> prevLetters = new ArrayList<Character>();&nbsp; &nbsp; for(int x = 0; x < input.length(); x++) {&nbsp; &nbsp; &nbsp; &nbsp; if(!prevLetters.contains(input.charAt(x))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int y = 0; y <= repetitions ; y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag += String.valueOf(input.charAt(x));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(input.contains(flag)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; replacement = flag.substring(0, flag.length()-1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(output.contains(flag)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output = output.replace(flag, replacement);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prevLetters.add(input.charAt(x));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return output;}这是我的解决方案,它遵循与您类似的想法。然而,与其比较每个字符值,我认为简单地检查规则中的中断(字符连续出现 n+1 次)并“修复”它会更容易。如果您有兴趣使用您的方法,我注意到的一个潜在问题是您没有在最后一个 else 中将计数分配给 1。您也没有机会添加最后一个字符,因为您只在循环持续时间为 len - 1 时在索引“i”处添加字符。

子衿沉夜

我认为你在正确的轨道上。我不确定这是否是一项作业,所以我不想直接给你答案,但这里有一些可能有用的提示:您已经在遍历字符串。这很棒!但是,我认为您想将当前字符与前一个字符进行比较,而不是下一个字符。您不需要将输入转换为 char 数组来迭代它,只需使用charAt(idx)您似乎从不使用 prev,但我认为您在声明时心中有正确的想法!将您的问题分为两部分:何时更新计数和何时附加字符。您可以在 for 循环中解决这两个问题,但不要尝试在同一个 if 语句中同时执行这两个操作,而是将其分解为多个 if。要做的三件事是:更新上一个值更新计数更新新字符串获得这些的正确顺序以及我将留给你的确切实现(再次,因为我不确定这是否是一项任务)更新:由于其他人发布,这是我的解决方案(使用单个 for 循环):private String replaceConsecutiveDuplicates(String input, int n) {&nbsp; &nbsp; if (input == null || input.length() < n) return input;&nbsp; &nbsp; if (n == 0) return "";&nbsp; &nbsp; StringBuffer sb = new StringBuffer();&nbsp; &nbsp; int count = 1;&nbsp; &nbsp; char prev = input.charAt(0);&nbsp; &nbsp; sb.append(prev);&nbsp; &nbsp; char current;&nbsp; &nbsp; for( int i = 1; i < input.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; current = input.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; if (prev == current) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (++count > n) continue;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; prev = current;&nbsp; &nbsp; &nbsp; &nbsp; sb.append(current);&nbsp; &nbsp; }&nbsp; &nbsp; return sb.toString();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java