Java中两个字符串的交集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4448370/
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
Intersection of two strings in Java
提问by Deepak
Need a Java function to find intersection of two strings. i.e. characters common to the strings.
需要一个 Java 函数来查找两个字符串的交集。即字符串共有的字符。
Example:
例子:
String s1 = new String("Sychelless");
String s2 = new String("Sydney");
采纳答案by Matthew Flaschen
Using HashSet<Character>
:
使用HashSet<Character>
:
HashSet<Character> h1 = new HashSet<Character>(), h2 = new HashSet<Character>();
for(int i = 0; i < s1.length(); i++)
{
h1.add(s1.charAt(i));
}
for(int i = 0; i < s2.length(); i++)
{
h2.add(s2.charAt(i));
}
h1.retainAll(h2);
Character[] res = h1.toArray(new Character[0]);
This is O(m + n)
, which is asymptotically optimal.
这是O(m + n)
,这是渐近最优的。
回答by Vicky
Found same question here, refer this
在这里发现了同样的问题,参考这个
Implementing an efficent algorithm to find the intersection of two strings
回答by Armen Tsirunyan
I think the algorithm you are looking for is the problem of the longest common subsequence
回答by AlexR
s1.contains(s2) returns true;
s1.indexOf(s2) returns 0.
s1.indexOf("foo") returns -1
For more sophisticated cases use class Pattern.
对于更复杂的情况,请使用类 Pattern。
回答by Jigar Joshi
Most basic approach:
最基本的做法:
String wordA = "Sychelless";
String wordB = "Sydney";
String common = "";
for(int i=0;i<wordA.length();i++){
for(int j=0;j<wordB.length();j++){
if(wordA.charAt(i)==wordB.charAt(j)){
common += wordA.charAt(i)+" ";
break;
}
}
}
System.out.println("common is: "+common);
回答by saugata
Extract the characters
提取字符
String.toCharArray
Put them in a Set Find the intersection
把它们放在一个集合中找到交集
Set.retainAll
回答by Jim Downing
More detail on saugata's response (appeared while I was writing this): -
关于 saugata 回应的更多细节(在我写这篇文章时出现):-
public static void main(String[] args) {
String s1 = "Seychelles";
String s2 = "Sydney";
Set<Character> ss1 = toSet(s1);
ss1.retainAll(toSet(s2));
System.out.println(ss1);
}
public static Set<Character> toSet(String s) {
Set<Character> ss = new HashSet<Character>(s.length());
for (char c : s.toCharArray())
ss.add(Character.valueOf(c));
return ss;
}
回答by Sergii Shevchyk
By means of Guava this task seems much easier:
借助 Guava,这项任务似乎要容易得多:
String s1 = new String("Sychelless");
String s2 = new String("Sydney");
Set<String> setA = Sets.newHashSet(Splitter.fixedLength(1).split(s1));
Set<String> setB = Sets.newHashSet(Splitter.fixedLength(1).split(s2));
Sets.intersection(setA, setB);
回答by Blasanka
I have used TreeSet
. And retainAll()
in TreeSet
to get matched elements.
我用过TreeSet
。而retainAll()
在TreeSet
获得匹配的元素。
Oracle Doc:
retainAll(Collection<?> c)
Retains only the elements in this set that are contained in the specified collection (optional operation).
甲骨文文档:
retainAll(Collection<?> c)
仅保留此集合中包含在指定集合中的元素(可选操作)。
String s1 = new String("Sychelless");
String s2 = new String("Sydney");
Set<Character> firstSet = new TreeSet<Character>();
for(int i = 0; i < s1.length(); i++) {
firstSet.add(s1.charAt(i));
}
Set<Character> anotherSet = new TreeSet<Character>();
for(int i = 0; i < s2.length(); i++) {
anotherSet.add(s2.charAt(i));
}
firstSet.retainAll(anotherSet);
System.out.println("Matched characters are " + firstSet.toString());//print common strings
//output > Matched characters are [S, e, y]
回答by Bishal Jaiswal
Optimized solution:
优化方案:
public static String twoStrings(String s1, String s2){
HashSet<Character> stringOne = new HashSet<Character>(), stringTwo = new HashSet<Character>();
int stringOneLength = s1.length();
int stringTwoLength = s2.length();
for(int i=0; i<stringOneLength || i<stringTwoLength; i++) {
if(i < stringOneLength)
stringOne.add(s1.charAt(i));
if(i < stringTwoLength)
stringTwo.add(s2.charAt(i));
}
stringOne.retainAll(stringTwo);
return stringOne.toString();
}