【问题标题】:The requirement is to remove three consecutive same characters from a String要求是从字符串中删除三个连续的相同字符
【发布时间】:2017-12-13 15:19:50
【问题描述】:
public static void bomberAlgo(String str)
    {
        String newString="";
        String givenString=str;
        for(int i=0;i<givenString.length()-1;i++)
        {
            if(givenString.charAt(i)!=givenString.charAt(i+1))
            {
             newString=newString+givenString.charAt(i);
            }
        }
        System.out.println("The new String is "+str);
    }

我的代码现在正在删除两个相同的字符。例如,如果输入字符串是“abcccddefgggh”,那么输出应该是“abddefh”

【问题讨论】:

  • 你有没有想过当你到达字符串的末尾时会发生什么?如果倒数第二个字符与最后一个字符相同,那么复制它的是什么?我确信这不是可能的输出 - 因为您实际上打印的是原始字符串,而不是新字符串。每年的产出是什么样的?

标签: java arrays string loops


【解决方案1】:

如果您确定要删除 3 个连续的字符,一个简单的方法是 -

StringBuffer sb = new StringBuffer();
 for (int i = 0; i < str.length(); i++) {
     if(i+2 < str.length() && str.charAt(i) == str.charAt(i+1) && str.charAt(i) == str.charAt(i+2)) {
          i+=2;
          continue;
     }
     sb.append(str.charAt(i));
 }
 System.out.println("The new String is "+ sb);

【讨论】:

    【解决方案2】:

    一种方法是在决定是否输出之前计算一个字符重复了多少次。见下文:

    static String removeConsecutive(String s, int minRepeatsToRemove) {
        int pos = 0;
        StringBuilder sb = new StringBuilder();
    
        while (pos < s.length()) {
            char c = s.charAt(pos);
            int repeats = 1;
            while (pos + repeats < s.length() && c == s.charAt(pos + repeats))
                repeats++;
    
            if (repeats < minRepeatsToRemove)
                for (int i = 0; i < repeats; i++)
                    sb.append(c);
    
            pos += repeats;
        }
    
        return sb.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(removeConsecutive("abcccddefgggh", 3));
    }
    

    【讨论】:

      【解决方案3】:
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package org.apache.log4j;
      
      import java.util.Scanner;
      
      public class Sample {
      
          public static void main(String ar[]) {
              Scanner sc = new Scanner(System.in);
              int len = sc.nextInt();
              String str;
      
              String[] arr = new String[len];
              for (int i = 0; i < len; i++) {
                  str = sc.next();
                  arr[i] = "";
                  String temp = str;
                  for (int j = 0; j < (temp.length() - 3);) {
                      if (temp.charAt(j) == temp.charAt(j + 1) && temp.charAt(j) == temp.charAt(j + 2)) {
                          temp = temp.substring(0, j) + "" + temp.substring(j + 3);
                          j = j + 3;
                      } else {
                          arr[i] = arr[i] + temp.charAt(j);
                          j = j + 1;                    
                      }
                  }
                  if (temp.length() > 2) {
                      if (arr[i].length() != 0) {
                          if (!(str.charAt(str.length() - 3) == str.charAt(str.length() - 2)) || !(str.charAt(str.length() - 3) == str.charAt(str.length() - 1))) {
                              if (!(str.charAt(str.length() - 3) == str.charAt(str.length() - 5)) || !(str.charAt(str.length() - 3) == str.charAt(str.length() - 4))) {
                                  arr[i] = arr[i] + str.charAt(str.length() - 3) + str.charAt(str.length() - 2) + str.charAt(str.length() - 1);
                              } else {
                                  arr[i] = arr[i] + str.charAt(str.length() - 2) + str.charAt(str.length() - 1);
                              }
                          }
                      }
                  } else {
                      arr[i] = temp;
                  }           
                  if (arr[i].length() == 0) {
                      System.out.println("-1");
                  } else {
                      System.out.println(arr[i]);
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-03
        • 2020-09-14
        • 2019-09-17
        相关资源
        最近更新 更多