Java - 如何检查字符串中的重复字符? [英] Java - How to check for duplicate characters in a string?

查看:231
本文介绍了Java - 如何检查字符串中的重复字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个函数来检查重复值的字符串,并返回唯一字符的数量。如果count大于3,则返回true。如果计数小于3,则应为假。这是我一直在尝试(注意我是新的java)

I need to write a function that checks a string for duplicate values and returns the count of unique characters. If the count is greater than 3, it should return true. If the count is less than 3, it should be false. Here is what I have been trying (notice I'm new to java)

private boolean isFormatValid(String password) {
    CharSequence inputStr = password;
    int length = inputStr.length();
    int numberDups = 0;

    for(int i=0; i < length; ++i) {
        Pattern pattern = Pattern.compile("(.)(?=.*?\1){1,20}");
        Matcher matcher = pattern.matcher(inputStr);
        numberDups += 1;
    }
    if (numberDups < 3) {
        return false;
    }
    return true;
}

我试图使用正则表达式,因为建议可能会更容易。但是如果我可以在没有正则表达式的情况下完成这个任务,我会更快乐。

I was trying to use a regex because it was suggested it might be easier. But if I can accomplish this without a regex I'd be happier.

这是什么意思?

private boolean isFormatValid(String password) {
    int length = inputStr.length();
    int numberChars = 0;

    for(int i=0; i < length; ++i) {
                int index = password.indexOf(i);
        CharArray[i] = charAt(i);   
    }
}

我觉得这甚至不是正确的...

I feel this isn't even close to being right...

推荐答案

我认为您的示例代码中的变量 numberDups 是错误的,这令人困惑的一些人。该变量应该表示不同的字符的数量,是不是?也就是说,如果字符串是 abcabc ,则数字将为 3 ,对于字符串 aaaaaaaaa 它将是 1

I think the variable numberDups in your sample code is misnamed, and that's confusing some people. That variable is supposed to represent the number of different characters, is it not? That is, if the string is abcabc the number would be 3, and for the string aaaaaaaaa it would be 1.

在这种情况下,最简单的解决方案是正如其他人所说,使用一套。实际上你的代码几乎在那里;只要摆脱 numberDups 计数器,并将其替换为 HashSet< Character> ,如下所示:

That being the case, the simplest solution is, as others have said, to use a Set. In fact your code is almost there; just get rid of that numberDups counter and replace it with a HashSet<Character>, like so:

static boolean isFormatValid(String password) {
    CharSequence inputStr = password;
    int length = inputStr.length();
    Set<Character> uniqueChars = new HashSet<Character>();

    for(int i=0; i < length; ++i) {
        uniqueChars.add(inputStr.charAt(i));
    }

    return uniqueChars.size() >= 3;
}

(但是,您不需要创建 charAt()和 length()密码变量中,因为 String 实现 CharSequence 接口。 )

(However, you don't need to create the inputStr variable. You can call CharSequence methods like charAt() and length() on the password variable because String implements the CharSequence interface.)

编辑:我也想指出,你使用的方式模式和匹配,你不是使用它们。您正确地从模式创建了匹配器,并将其与输入字符串相关联,但是它只是坐在那里。为了应用正则表达式,您必须调用其中一种方法, find() matches()(或 lookingAt(),但没有人使用过那个)。

I also want to point out that, the way you were using the Pattern and Matcher, you weren't using them. You correctly created the Matcher from the Pattern, and associated it with the input string, but then it just sat there. In order to apply the regex, you have to call one of the methods, find() or matches() (or lookingAt(), but nobody ever uses that one).

一个很常见的初学者的错误。 Java的声誉过于冗长,但在这种情况下,它尤为明显(令人惊讶)。我的意思是什么是正则表达式,如果不是让你解决问题,而不用编写代码的?但这并不总是那么糟糕;这里是使用正则表达式的单行解决方案:

That's is a very common beginner's mistake. Java has a reputation for being excessively verbose anyway, but it's especially noticeable (and surprising) in this case. I mean, what are regexes for, if not to let you solve problems without writing reams of code? But it's not always that bad; here's a one-line solution using a regex:

return inputStr.replaceAll("(.)(?=.*\\1)", "").length() >= 3;

也就是删除所有的重复项,生成的字符串的长度与数字相同的独特人物。虽然基于集合的解决方案仍然更简单;这一个只是更短。

That is, remove all the duplicates, and the length of the resulting string is the same as the number of unique characters. The set-based solution is still simpler, though; this one is just shorter.

这篇关于Java - 如何检查字符串中的重复字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆