简体   繁体   English

如何构造正则表达式以平衡字符串中的字符?

[英]How to construct regular expression to balance characters in a string?

I have come across regular expressions for different problems but I could not find out regex s to balance characters in a string. 我遇到了各种不同问题的正则表达式,但是我找不到正则表达式来平衡字符串中的字符。

I came across a problem, to find if a string is balanced. 我遇到了一个问题,要查找字符串是否平衡。 ex: aabbccdd is a balanced one, as a characters are repeated in even numbers but aabbccddd is not a balanced one since ddd is repeated in odd number mode. 例如: aabbccdd是一个平衡的字符,因为一个字符以偶数重复,但是aabbccddd aabbccdd是一个平衡的字符,因为ddd在奇数模式下重复。 This is applicable for all characters give an input not to specific a,b,c and d . 这适用于所有不输入特定a,b,c and d字符。 If i give input as 12344321 or 123454321 , it should return balanced and unbalanced result respectively. 如果我输入为12344321123454321 ,则应分别返回平衡和不平衡的结果。

How to find the balance using regex. 如何使用正则表达式查找余额。 What type of regular expression we should use to find if the string is balanced? 我们应该使用哪种类型的正则表达式来查找字符串是否平衡?

Edit:

I tried to find solution using regex only as the problem demands answer in regex pattern. 我试图仅使用正则表达式来查找解决方案,因为问题需要以正则表达式模式回答。 I would implemented using any other solution if regex was not mentioned explicitly 如果未明确提及正则表达式,我将使用任何其他解决方案来实现

I don't think you can do it with regex. 我认为您不能使用正则表达式来做到这一点。 Why do you need to use them? 为什么需要使用它们? I tried this: it works and it's pretty simple 我试过了:很有效,而且很简单

static boolean isBalanced(String str) {
    ArrayList<Character> odds = new ArrayList<>(); //Will contain the characters read until now an odd number of times
    for (char x : str.toCharArray()) { //Reads each char of the string
        if (odds.contains(x)) { //If x was in the arraylist we found x an even number of times so let's remove it
            odds.remove(odds.indexOf(x));
        }
        else {
            odds.add(x);
        }
    }
    return odds.isEmpty();
}

Regular expression for this problem exists, but doesn't speed up anythings and will be totally messy. 存在此问题的正则表达式,但不能加快速度,并且会变得非常混乱。 It's easier to prepare NFA, and then switch to REGEX. 准备NFA,然后切换到REGEX,会更容易。 Still, it's not proper tool. 尽管如此,它还是不合适的工具。

public static void main(String args[]) {
    String s = args[0];
    int[] counter = new int[256];
    for (int i = 0; i < s.length(); i++) {
        counter[s.charAt(i)]++;
    }
    if (validate(counter)) {
        System.out.println("valid");
    } else {
        System.out.println("invalid");
    }
}

public static boolean validate(int[] tab) {
    for (int i : tab) {
        if (i%2 == 1) {
            return false;
        }
    }
    return true;
}

Edit: for pointing the regex existance 编辑: 用于指出正则表达式的存在

Reference for a finite automate for just two characters. 对于仅两个字符的有限自动化的参考。 Start on the very left, win with double circle. 从最左边开始,赢双圈。 Each state named by the set of characters that have odd count so far. 到目前为止,由字符集命名的状态具有奇数计数。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM