java 如何比较包含相同字符的2个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3550469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:21:57  来源:igfitidea点击:

How to compare 2 strings containing same characters

java

提问by prasad

My question is that, I have 2 strings, say String1 & String2. Now I want to check whether these 2 strings contain same characters or not, irrespective of their sequence.

我的问题是,我有 2 个字符串,比如 String1 和 String2。现在我想检查这两个字符串是否包含相同的字符,无论它们的顺序如何。

Suppose String1= "qwerty", String2= "qywter". Now these Strings contain same characters but are in different sequence. So is there any function that can be used to show that these strings contain same characters?? Can equals() method do that???

假设String1= "qwerty"String2= "qywter"。现在这些字符串包含相同的字符,但顺序不同。那么有没有什么函数可以用来显示这些字符串包含相同的字符呢??equals() 方法可以做到吗???

All help is appreciated.

感谢所有帮助。

回答by Bozho

char[] chars1 = string1.toCharArray();
char[] chars2 = string2.toCharArray();
Arrays.sort(chars1);
Arrays.sort(chars2);

return Arrays.equals(chars1, chars2);

回答by polygenelubricants

You can use String.equals, albeit indirectly. First you need a helper method:

您可以使用String.equals,尽管是间接的。首先你需要一个辅助方法:

// given a String, sorts its chars and return it as another String
public static String sorted(String s) {
    char[] arr = s.toCharArray();
    Arrays.sort(arr);
    return new String(arr);
}

Then you can have:

然后你可以有:

    String s1 = "qwerty";
    String s2 = "qywter";

    System.out.println(sorted(s1)); // eqrtwy

    System.out.println(sorted(s1).equals(sorted(s2))); // true

Note that this is not the most efficient algorithm -- it's O(N log N)time, and uses extraneous space -- but should work fine for short strings. For long strings, you'd want to go through each char(or Unicode code points) manually (instead of toCharArray()), and perhaps use the linear-time counting sort.

请注意,这不是最有效的算法——它是O(N log N)时间,并且使用了额外的空间——但对于短字符串应该可以正常工作。对于长字符串,您需要char手动(而不是toCharArray())遍历每个(或 Unicode 代码点),并且可能使用线性时间计数 sort

If you don't care about specific character counts matching (e.g. "xxxyyy"and "xy"has the same chars, albeit in different numbers), then you can use a set-like representation (java.util.BitSet).

如果您不关心特定的字符数匹配(例如,"xxxyyy"并且"xy"具有相同的字符,尽管数字不同),那么您可以使用类似集合的表示 ( java.util.BitSet)。

// given a string, returns its used char set as a java.util.BitSet
public static BitSet usedChar(String s) {
    BitSet bs = new BitSet();
    for (int i = 0; i < s.length(); i++) {
        bs.set(s.charAt(i));
    }
    return bs;
}

Then you can have:

然后你可以有:

    System.out.println(
        usedChar("xxxyyy").equals(usedChar("xy"))
    ); // true

    System.out.println(
        usedChar("xyz").equals(usedChar("abc"))
    ); // false

回答by Brett Kail

It depends whether you actually want characters or you really want code points, and then it matters whether you want to count duplicates or not. Here's one solution:

这取决于您是真的想要字符还是真的想要代码点,然后重要的是您是否要计算重复项。这是一种解决方案:

public class a {
  public static void main(String[] args) {
    String s1 = "qwerty";
    String s2= "qywter";
    System.out.println(codePointSet(s1).equals(codePointSet(s2)));
  }
  public static Set<Integer> codePointSet(String s) {
    Set<Integer> set = new TreeSet<Integer>();
    for (int i = 0, cp; i < s.length(); i += Character.charCount(i)) {
      cp = s.codePointAt(i);
      set.add(cp);
    }
    return set;
  }
}

回答by jethro

int[] f = new int[(int)char.MaxValue];
foreach (var c in string1) f[(int)c]++;
foreach (var c in string2) f[(int)c]--;
return f.Max() == 0 && f.Min() == 0;

This is preferable solution when string1.length() >> char.MaxValue and it has lower big O notation complexity.

这是 string1.length() >> char.MaxValue 时的首选解决方案,它具有较低的大 O 符号复杂度。

EDITthis is actually C# code but you can easly achieve similar result in Java.

编辑这实际上是 C# 代码,但您可以在Java.

回答by McTrafik

If you have a long string that you need to compare, and you don't need a guarantee of success, you can do something like this:

如果您有一个需要比较的长字符串,并且不需要保证成功,您可以执行以下操作:

  1. make sure the strings are the same length
  2. for each image
  3. add up all the characters (casted as ints)
  4. add up squares of characters (again casted as ints)
  5. compare the sums of squares and the sums
  6. if they are the same, then the strings contain the same characters.
  1. 确保字符串长度相同
  2. 对于每个图像
  3. 将所有字符相加(转换为整数)
  4. 将字符的平方相加(再次转换为整数)
  5. 比较平方和和总和
  6. 如果它们相同,则字符串包含相同的字符。

Actually I spent some time trying to figure out where this wouldn't work, but I can't think of one. My gut tells me I'm missing something here, or this is a good comparator for this case.

实际上我花了一些时间试图找出这不起作用的地方,但我想不出一个。我的直觉告诉我我在这里遗漏了一些东西,或者这是这种情况下的一个很好的比较器。

回答by Vikash kumar

Two steps are require

需要两个步骤

  1. Do xor of both strings and if xor is 0 then you are partially sure.

  2. If xor is 0 then find the sum of ascii value of both strings and if ascii sum is same then both strings are same.

  1. 对两个字符串进行异或,如果异或为 0,则您可以部分确定。

  2. 如果 xor 为 0,则找到两个字符串的 ascii 值的总和,如果 ascii sum 相同,则两个字符串相同。

Hope this helps

希望这可以帮助

回答by Bernard

String.equals()won't work for your particular case. You will likely need to write your own method to equate strings in this way.

String.equals()不适用于您的特定情况。您可能需要编写自己的方法来以这种方式将字符串等同起来。