sefirosu
sefirosu

Reputation: 2648

compare 2 strings, see if they are containing common items or not

say i have 2 strings, Boolean hasName = false;

String employeeNames = "lee,anne,peter,sam,paul";
String managerNames = "ken,lee,sue,tim,alex";

so, what s the best way to iterate through 2 strings and find out both strings contains "lee", then i can set

hasName = true;

these are just a simple sample.. i dont want answer such as employeeNames.contains("lee") or something like that, because my data in real product are all dynamic and can be very long string...i wont know what i am getting, but as soon as i received 2 strings, i need to find out they r contianning at least one common item or not... what is the best way to this algrothoim? one more thing , the items in string all separete by "," so is there anything i can do ? thanks

Upvotes: 0

Views: 1197

Answers (5)

Alessio
Alessio

Reputation: 2068

This has complexity O(N*M), where N and M are the number of strings in the two strings

public Boolean hasNames(String s1, String s2){
    List<String> s1List = Arrays.asList(s1.split(","));
    List<String> s2List = Arrays.asList(s2.split(","));

    for(String s : s1List)
        if(s2List.indexOf(s)>=0)
            return true;
    for(String s : s2List)
        if(s1List.indexOf(s)>=0)
            return true;
    return false;
}

Another option

public Boolean hasNames(String s1, String s2){
    Set<String> s1Set = new HashSet<String>();
    List<String> s2List = Arrays.asList(s2.split(","));
    for(String s : s1.split(","))
        s1Set.add(s);
    s1Set.retainAll(s2List);
    return !s1Set.isEmpty();
}

Upvotes: 1

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13525

public Boolean hasNames(String s1, String s2){
  List<String> s1List = Arrays.asList(s1.split(","));
  List<String> s2List = Arrays.asList(s2.split(","));
  HashSet set=new HashSet(s1List);
  set.retainAll(s2List);
  return !set.isEmpty();
}

Complexity is O(M*ln(N)).

Upvotes: 1

Mohammed Alokshiya
Mohammed Alokshiya

Reputation: 643

Use StringTokenizer Class to split all words in the first string and insert them all in a HashSet. For the second string, check if any word is in the hashSet or not. The complexity of this code is O(max(N, M)) ~= O(N).

String employeeNames = "lee,anne,peter,sam,paul";
String managerNames = "ken,lee,sue,tim,alex";
boolean hasName = false;

HashSet<String> hash = new HashSet<>();
StringTokenizer st1 = new StringTokenizer(employeeNames);
while (st1.hasMoreTokens()) {
    hash.add(st1.nextToken());
}

StringTokenizer st2 = new StringTokenizer(managerNames);
while (st2.hasMoreTokens()) {
    if (hash.contains(st2.nextToken())) {
        hasName = true;
        break;
    }
}

Upvotes: 1

Drifter64
Drifter64

Reputation: 1123

Try something like this:

public static boolean hasName()
{
    boolean value = false;
    String[] empArr = employeeNames.split(",");
    String[] manArr = managerNames.split(",");

    for (int i = 0; i < empArr.length; i++)
    {
        String s = empArr[i];

        for int j = 0, j < manArr.length; j++)
        {
            String t = manArr[j];
            if (s.equals(t)) { value = true; }
        }
    }

    return value;
}

This loops through both lists and tries to find something that is both in the first and second list!

Upvotes: 1

Olivier Croisier
Olivier Croisier

Reputation: 6149

You can use the de-duplicating property of Sets to compare the contents of your two lists of names :

public static boolean hasNames(String s1, String s2) {
    List<String> s1List = Arrays.asList(s1.split(","));
    List<String> s2List = Arrays.asList(s2.split(","));
    HashSet<String> names = new HashSet<>(s1List);
    names.addAll(s2List);
    return names.size() < s1List.size() + s2List.size();
}

Explanation : if the names are different in both lists, they will all be added to the set, so the size of the set will be equal to the sum of both lists. However, if they have one or more common terms, the set will wipe them away and therefore contain less elements than the sum of the two lists.

Plus, HashSets are using hashcodes, which makes them much faster than using List's contains(), which is in O(N).

Upvotes: 2

Related Questions