Java中两个字符串的交集

时间:2010-12-15 09:20:24

标签: java algorithm intersection

需要Java函数来查找两个字符串的交集。即字符串共有的字符。

示例:

String s1 = new String("Sychelless");
String s2 = new String("Sydney");

10 个答案:

答案 0 :(得分:22)

使用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]);

这是O(m + n),这是渐近最优的。

答案 1 :(得分:8)

提取字符

String.toCharArray

将它们放入一套 找到交叉点

Set.retainAll

答案 2 :(得分:5)

最基本的方法:

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);  

答案 3 :(得分:3)

关于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;
}

答案 4 :(得分:2)

我认为您正在寻找的算法是problem of the longest common subsequence

答案 5 :(得分:1)

答案 6 :(得分:1)

优化的解决方案

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();
}

答案 7 :(得分:0)

通过番石榴,这项任务似乎更容易:

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);

答案 8 :(得分:0)

我使用过TreeSetTreeSetretainAll()获取匹配的元素。

  

Oracle Doc:

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]

答案 9 :(得分:-6)

s1.contains(s2) returns true;
s1.indexOf(s2) returns 0. 
s1.indexOf("foo") returns -1

对于更复杂的案例,请使用类Pattern。