Bigote
Bigote

Reputation: 95

check if each of string is present in another however the order

I would like to compare two strings (str1 and str2) to know if all elements of my str2 are contained by str1.

To be more specific, please look at the following example:

I have:

str1="1,3"
str2= "1,2,3,5"

if(str2.contains(str1)
    System.out.print("YES");
else 
    System.out.print("NO");

My console displays "NO". However, 1 and 3 are present in str2. Do you know how to know if each element of my str1 is present in my str2 however the order?

Upvotes: 3

Views: 98

Answers (3)

Meghna
Meghna

Reputation: 39

S1="1,3" is the string, there are no elements in the string.

if(str2.contains(str1)) implies if "1,3" is present in S2 i.e. "1,2,3,5", which is not hence its returning false. I matches the complete string and not the character of string

You can convert the string to a list of character/Integer and check if all elements in that list are present in the string S2.

Code:

    String s1 = "1,2";
    String s2 = "1,2,3,4";
    String s3 = "1,5";
    List<String> s1List = new ArrayList<>(Arrays.asList(s1.split(",")));
    List<String> s2List = new ArrayList<>(Arrays.asList(s2.split(",")));
    System.out.println(s1List);
    if(s1List.stream().allMatch(s2List::contains))
        System.out.print("YES");
    else
        System.out.print("NO");

    List<String> s3List = new ArrayList<>(Arrays.asList(s3.split(",")));
    System.out.println(s3List);
    if(s3List.stream().allMatch(s2List::contains))
        System.out.print("YES");
    else
        System.out.print("NO");

Output:

[1, 2]
YES
[1, 5]
NO

Upvotes: 0

M. Prokhorov
M. Prokhorov

Reputation: 3993

There are two general cases here:

  1. You don't care about duplicates. Meaning that 1,2,3,4 does contains 1,3,3.
  2. You do care about duplicates. That means 1,3,4,5 contains 1,4, but not 1,1,4.

Given

String str1 = "1,3,3";
String str2 = "1,2,3,4,5";

For first case.

All you have to do is make two Sets, and ask "bigger" set if it containsAll elements from the "lesser" set:

Set<String> dict = Set.of(str2.split(","));
Set<String> input = Set.of(str1.split(","));

if (dict.containsAll(input)) {
  print("Contains");
}
else {
  print("Doesn't contain");
}

For second case

You can also use a set, but you also need to know the cardinality of each of the elements. That means you want a Map:

Map<String, Long> elementCardinality(String[] input) {
  return Arrays.stream(input)
    .collect(Collectors.groupingBy(
        Function.identity(),
        Collectors.counting()));
}

And then for your two strings:

Map<String, Long> dict  = elementCardinality(str2.split(","));
Map<String, Long> input = elementCardinality(str1.split(","));

boolean containsAll = true;
for (Map.Entry<String,Long> entry : input.entrySet()) {
  Long dictCardinality = dict.getOrDefault(entry.getKey(), 0L);
  if (!dictCardinality.equals(entry.getValue())) {
    containsAll = false;
    break;
  }
}
if (containsAll) {
  print("Contains");
}
else {
  print("Doesn't contain");
}

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

We can try converting both CSV strings to sets, and then using Set#containsAll to check if one set contain the other:

Set<String> set1 = new HashSet<>(Arrays.asList(str1.split(",")));
Set<String> set2 = new HashSet<>(Arrays.asList(str2.split(",")));
if (set2.containsAll(set1)) {
    System.out.print("YES");
}
else {
    System.out.print("NO");
}

Upvotes: 4

Related Questions