一定次数后删除字符

时间:2018-11-18 01:22:45

标签: java

因此,从本质上讲,我尝试删除某个重复次数已过的字符。因此,给定一个字符串和一个整数作为允许的出现次数,在达到该数目后将其删除。

例如:DeleteChars("abaabbb",2);应该输出:“ abab”

我无法使其完全正常工作,我真的不知道我要去哪里。我觉得我所拥有的应该可以工作。但是我对正则表达式不是很好,所以我确定这就是问题所在,但我真的不知道如何解决。

public static String DeleteChars(String data, int deleteValue)
{
    String regex = "(.)\\1{" + deleteValue + ",}";

    StringBuffer sbuffer = new StringBuffer();
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(data);
    while(m.find())
    {
        m.appendReplacement(sbuffer, "");
    }
    m.appendTail(sbuffer);
    return sbuffer.toString();
}

编辑:为I / O添加更多案例以获得所需的输出。

DeleteChars("ababababababa", 2);
Expected: abab

DeleteChars("abcabcaabbcceess", 1)
Expected: abces

DeleteChars("Hey there are spaces", 2)
Expected: Hey ther arspacs

DeleteChars("absdfsdfgldfj", 0)
Expected:

2 个答案:

答案 0 :(得分:0)

在这种情况下,无需使用正则表达式。您可以将Map中的每个字符都添加到和整数(代表字符串中出现的次数),然后检查是否应添加或忽略(请查看代码注释以获取更多信息)。

摘要:

import java.util.HashMap;

public class Snippet {

    public static void main(String[] args) {
        System.out.println("output = " + DeleteChars("abaabbb", 2 ));
        System.out.println("output = " + DeleteChars("abcabcaabbcceess", 1));
        System.out.println("output = " + DeleteChars("Hey there are spaces", 2));
        System.out.println("output = " + DeleteChars("absdf\nsdf\ngld\nfj", 2));

    }

    public static String DeleteChars(String data, int deleteValue)
    {
        //map characters with Integers example {a=3, b=4}
        HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>(); 
        //array of string data
        char[] strArray = data.toCharArray(); 
        //output (empty at first)
        String output = "";
        for (char c : strArray) { 
            if (charCountMap.containsKey(c)) { 
                // If char is present in charCountMap, 
                // incrementing it's count by 1 
                charCountMap.put(c, charCountMap.get(c) + 1); 
            } 
            else { 
                // If char is not present in charCountMap, 
                // putting this char to charCountMap with 1 as it's value 
                charCountMap.put(c, 1); 
            }
            //if not reached max ocurrences just add the char to output string
            if(charCountMap.get(c) <= deleteValue) output = output + c;
        } 
        //print map (just informative)
        System.out.println("Map = " + charCountMap.toString().replaceAll("\n","linebreak").replaceAll(" =", "space ="));
        return output;
    }

}

输出:

Map = {a=3, b=4}
output = abab
Map = {a=4, b=4, c=4, s=2, e=2}
output = abces
Map = {space =3, p=1, a=2, r=2, s=2, c=1, t=1, e=5, H=1, h=1, y=1}
output = Hey ther arspacs
Map = {a=1, b=1, s=2, d=3, f=3, g=1, linebreak=3, j=1, l=1}
output = absdf
sdf
glj

它将与您的测试用例匹配,并处理换行符和空格。

答案 1 :(得分:0)

如果您正在寻找不使用正则表达式的解决方案,则此选项使用哈希图来存储字符串中每个字符的数量。这将适用于任何字符串,而不仅仅是字母。

    public static String deleteChars(String data, int deleteValue) {
    HashMap<Character, Integer> compareMap = new HashMap<>();
    String returnString = "";

    // Iterate through the string. Use a hashmap to store the number of characters in the string as you iterate
    // through. Add chars until you reach the limit.
    for (int i = 0; i < data.length(); i++) {
        char charToAdd = data.charAt(i);
        Integer numberOfChars = compareMap.get(charToAdd);
        Integer valueToAdd = (numberOfChars == null) ? 1 : numberOfChars+1;
        compareMap.put(charToAdd, valueToAdd);

        //After incrementing the hashmap value, we check to see if we can add this to the return string
        if (compareMap.get(charToAdd) <= deleteValue) {
            returnString = returnString.concat(String.valueOf(charToAdd));
        }
    }
    return returnString;
}