java 接受 2 个字符串并在其中显示常见字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30662384/
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
Accept 2 Strings and display common chars in them
提问by suraj kiran
I am using the code below to find common characters in two strings. Sometimes this code produces wrong results such as giving output value which is greater than string lengths.
我正在使用下面的代码来查找两个字符串中的常见字符。有时此代码会产生错误的结果,例如给出大于字符串长度的输出值。
for(int i = 0;i < num1.length();i++){
for(int j =0;j < num2.length();j++){
if(num1.charAt(i) == num2.charAt(j)){
count++;
}
}
}
回答by mastov
It's not clear what you are trying to achieve.
目前尚不清楚您要实现的目标。
Your code can produce results greater than the string length because of characters that appear more than once in your strings. You can get results of up to num1.length() * num2.length().
由于在字符串中出现多次的字符,您的代码可以产生大于字符串长度的结果。您可以获得最多 num1.length() * num2.length() 的结果。
In case you want to get the number of positions at which you have the same character in both strings, you can to that in just one loop and use the same index for the "charAt" calls on both strings:
如果您想获得两个字符串中具有相同字符的位置数,您可以在一个循环中获得该数量,并为两个字符串上的“charAt”调用使用相同的索引:
for(int i = 0; i < num1.length() && i < num2.length(); i++) {
if(num1.charAt(i) == num2.charAt(i)){
count++;
}
}
In case you want to get the number of unique letters that appear in both strings, run through both strings independently and add the letters to a set. Then intersect both sets. The number of elements in that intersection set is your result:
如果您想获得出现在两个字符串中的唯一字母的数量,请独立运行两个字符串并将这些字母添加到一个集合中。然后将两组相交。该交集中的元素数是您的结果:
Set<Character> characters1 = new TreeSet<Character>();
for(int i = 0; i < num1.length(); i++) {
characters1.add(num1.charAt(i));
}
Set<Character> characters2 = new TreeSet<Character>();
for(int i = 0; i < num2.length(); i++) {
characters2.add(num2.charAt(i));
}
characters1.retainAll(characters2);
return characters1.size();
回答by almightyGOSU
You can try something like this using HashSet
您可以使用HashSet尝试类似的操作
import java.util.HashSet;
import java.util.Set;
public class QuickTester {
public static void main(String[] args) {
String s1 = "happy";
String s2 = "elephant";
Set<Character> set1 = new HashSet<Character>();
Set<Character> set2 = new HashSet<Character>();
for(char c : s1.toCharArray()) {
set1.add(c);
}
for(char c : s2.toCharArray()) {
set2.add(c);
}
// Stores the intersection of set1 and set2 inside set1
set1.retainAll(set2);
for(char c : set1) {
System.out.print(" " + c);
}
System.out.println("\nTotal number of common characters: "
+ set1.size());
}
}
Refer to retainAllon how the intersection of 2 sets is done.
关于如何完成 2 个集合的交集,请参阅retainAll。
Input Strings:
输入字符串:
happy
elephant
Output:
输出:
p a h
Total number of common characters: 3
回答by slarge
Use org.apache.commons.lang.StringUtils to count matches like this
使用 org.apache.commons.lang.StringUtils 来计算这样的匹配项
String num1 = "Java";
String num2 = "Guava";
int count = 0;
List<String> charsChecked = new ArrayList<>();
for(int i = 0;i < num1.length();i++){
String charToCheck = num1.substring(i, i+1);
if (!charsChecked.contains(charToCheck)) {
count += StringUtils.countMatches(num2, charToCheck);
charsChecked.add(charToCheck);
}
}
System.out.println(count);
This results in the count being 3 in the above example
这导致上例中的计数为 3