如何保留匹配的子字符串并替换Java String中不匹配的子字符串

时间:2016-08-06 21:16:45

标签: java string algorithm data-structures arr

你好我尝试在一个字符串数组中打印 通过以下方式:

  • 输入:big =“12xy34”,small =“xy”输出:“** xy **”
  • 输入:big =“”12xt34“”,small =“xy”输出:“******”
  • 输入:big =“12xy34”,small =“1”输出:“1 *****”
  • 输入:big =“12xy34xyabcxy”,small =“xy”输出:“** xy ** xy *** xy”
  • 输入:big =“78abcd78cd”,small =“78”输出:“78 **** 78 **”

我需要写一个条件来接收吗?

 public static String stars(String big, String small) {
      //throw new RuntimeException("not implemented yet ");
     char[] arr = big.toCharArray();
    for (int i = 0; i < arr.length; i++) {
        if (big.contains(small) ) {
            arr[i] = '*';
        }
    }
        String a = Arrays.toString(arr);
     return big+""+a;

public static String stars(String big, String small) { //throw new RuntimeException("not implemented yet "); char[] arr = big.toCharArray(); for (int i = 0; i < arr.length; i++) { if (big.contains(small) ) { arr[i] = '*'; } } String a = Arrays.toString(arr); return big+""+a;

1 个答案:

答案 0 :(得分:1)

<强>算法:

  1. 分别将bigsmall String's转换为char[]数组&#39; bigCsmallC
  2. 迭代big String
  3. 的每个字符
  4. 在迭代期间的每个索引处,确定是否存在可能的开始当前字符的子字符串
  5. 如果存在子字符串的可能性,请将big字符串迭代中的索引推进small String
  6. 的长度
  7. 否则,请用*
  8. 替换字符

    <强>代码:

    public class StringRetainer {
    
        public static void main(String args[]) {
            String big[] = {"12xy34", "12xt34", "12xy34", "12xy34xyabcxy", "78abcd78cd"};
            String small[] = {"xy", "xy", "1", "xy", "78"};
            for(int i = 0; i < big.length & i < small.length; i++) {
                System.out.println("Input: big = \"" + big[i] + "\", small = \"" + small[i] + "\" output : \"" + stars(big[i], small[i]) + "\"");
            }
        }
    
        public static String stars(String big, String small) {
            //String to char[] array conversions
            char[] bigC = big.toCharArray();
            char[] smallC = small.toCharArray();
            //iterate through every character of big String and selectively replace
            for(int i = 0; i < bigC.length; i++) {
                //flag to determine whether small String occurs in big String
                boolean possibleSubString = true;
                int j = 0;
                //iterate through every character of small String to determine
                //the possibility of character replacement
                for(; j < smallC.length && (i+j) < bigC.length; j++) {
                    //if there is a mismatch of at least one character in big String
                    if(bigC[i+j] != smallC[j]) {
                        //set the flag indicating sub string is not possible and break
                        possibleSubString = false;
                        break;
                    }
                }
                //if small String is part of big String,
                //advance the loop index with length of small String
                //replace with '*' otherwise
                if(possibleSubString)
                    i = i+j-1;
                else
                    bigC[i] = '*';
            }
            big = String.copyValueOf(bigC);
            return big;
        }
    
    }
    

    注意:

    1. 这是一种可能的解决方案(传统的做法)
    2. 使用内置String / StringBuffer / StringBuilder方法
    3. 看起来没有直接的方法来实现这一目标