0

I am having 2 lists

allWords [book, pen, pencil] 
subsetString [book  pen  , book  pencil  , pen  pencil  ]

I am expecting my output as

book  pen => pencil
book  pencil => pen
pen  pencil => book

ie for each element in subsetString I will be checking it with allwords. Once a match is not found that string from allwords will be added to RHS of output

But my issue is Now I am only getting 2 output instead of 3

allWords [book, pen, pencil]
subsetString [book  pen  , book  pencil  , pen  pencil  ]
pen  pencil   => book
book  pen   => pencil

The reason is while coming into book pencil it get checked with allWords [book, pen, pencil]

once it comes to pen contains book pencil - It is getting satisfed (as pencil contains a substring pen).

CODE

public void getStrongRules2(ArrayList<String> allWords,String delim) {
        ArrayList<ArrayList<String>> subsets = BTSum(allWords);
        ArrayList<String> subsetString = new ArrayList<String>();

        for (int j = 0; j < subsets.size(); j++) {
            String substring = "";
            for (int k = 0; k < subsets.get(j).size(); k++) {

                substring += subsets.get(j).get(k) + "  ";
            }
            subsetString.add(substring);
        }
        System.out.println("allWords "+allWords);
        System.out.println("subsetString "+subsetString);

        for(String a : allWords){
            for (int j = 0; j < subsetString.size(); j++) {
                if (!(subsetString.get(j).contains(a))) {
                    System.out.println(subsetString.get(j)+" => "+a);
                }
            }
        }
    }
    public static ArrayList<ArrayList<String>> BTSum(ArrayList<String> numbers) {

        int n = numbers.size();
        ArrayList<ArrayList<String>> powerSet = new ArrayList<ArrayList<String>>();

        for (long i = 0; i < (1 << n); i++) {
            ArrayList<String> element = new ArrayList<String>();
            for (int j = 0; j < n; j++)
                if ((i >> j) % 2 == 1) {
                    element.add(numbers.get(j));
                }
            if (element.size() > 1 && element.size() < n) {
                powerSet.add(element);
            }
        }
        return powerSet;
    }

}

But this should not happen in my case.

How to rectify that.

Please Suggest

USB
  • 6,019
  • 15
  • 62
  • 93

7 Answers7

1

Consider

     List<String> allWords = new ArrayList<>();
     allWords.add("Book"); allWords.add("Pen"); allWords.add("Pencil") ; 

If you can able to Split your Subset into three different lists instead of a single arrayList, Do split Like

     list1 [Book,Pen] 
     list2 [Book,Pencil]
     list3 [Pen,Pencil]

You can follow this without doing loops

     Set<String> results1 = new HashSet<String>(allWords);
     results1.removeAll(list1);
     System.out.println("result1 : "+ list1 +" ===> "+ results1);

     Set<String> results2 = new HashSet<String>(allWords);
     results2.removeAll(list2);
     System.out.println("result2 : "+ list2 +" ===> "+ results2);

     Set<String> results3 = new HashSet<String>(allWords);
     results3.removeAll(list3);
     System.out.println("result3 : "+ list3 +" ===> "+ results3);

Output

result1 : [Book, Pen] ===> [Pencil]
result2 : [Book, Pencil] ===> [Pen]
result3 : [Pen, Pencil] ===> [Book]

Hope this helps

Sri
  • 1,505
  • 2
  • 18
  • 35
0

Just sort your substring by length desc and then look for them. use custom comprator like:

public class MyComparator implements java.util.Comparator<String> {

    private int referenceLength;

    public MyComparator(String reference) {
        super();
        this.referenceLength = reference.length();
    }

    public int compare(String s1, String s2) {
        int dist1 = Math.abs(s1.length() - referenceLength);
        int dist2 = Math.abs(s2.length() - referenceLength);

        return dist1 - dist2;
    }
}

and sort your substring array of keyword by it

java.util.Collections.sort(List, myComparator )
jdev
  • 5,304
  • 1
  • 17
  • 20
0

This is because (subsetString.get(j).contains(a)) ->> "pen pencil".contains('pen') is true. Hence it is not going inside your if loop to print result.

PatSham
  • 33
  • 6
0

The issue is book pencil 'pen'cil contains pen So you can't get correct result using contains().

"book pencil".contains("pen") ===> true

So you can try something like following

 ArrayList<String> allWords = new ArrayList<String>() {{
        add("book");
        add("pen");
        add("pencil");
    }};
 ArrayList<String> subsetString = new ArrayList<String>() {{
        add("book  pen");
        add("book  pencil");
        add("pen  pencil");
    }};

 for (String i : subsetString) {
      List<String> stringList = Arrays.asList(i.split(" "));
        for (String j : allWords) {
            if(!stringList.contains(j)) {
               System.out.println(i + " => " + j);
            }
        }
  }  
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

You don't need all that code. Just use List.removeAll() to do all the heavy lifting:

List<String> allWords = Arrays.asList("book", "pen", "pencil");
List<List<String>> subsetString = Arrays.asList(
    Arrays.asList("book", "pen"), 
    Arrays.asList("book", "pencil"),
    Arrays.asList("pen", "pencil"));

for (List<String> subset : subsetString) {
    ArrayList<String> strings = new ArrayList<String>(allWords);
    strings.removeAll(subset);
    System.out.println(subset + " => " + strings);
}

Output:

[book, pen] => [pencil]
[book, pencil] => [pen]
[pen, pencil] => [book]
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can do something like this:

    List<String> allWords = new ArrayList<String>();
    allWords.add("book");
    allWords.add("pen");
    allWords.add("pencil");

    List<String> subsetStrring = new ArrayList<String>();
    subsetStrring.add("book pen");
    subsetStrring.add("book pencil");
    subsetStrring.add("pen pencil");

    for (String string : subsetStrring) {
        List<String> subsetStr = Arrays.asList(string.split(" "));
        for (String word : allWords) {
            if(!subsetStr.contains(word)) {
                System.out.println(string + " => " + word);
                break;
            }
        }
    }
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

I think use equals to match string can avoid the subString problem

for(String a : allWords)
{
    for (int j = 0; j < subsets.size(); j++) 
    {
        boolean isMatch = false;
        String subSetString = "";
        for(int k = 0 ; k < subsets.get(j).size() ; k++)
        {
            if(subsets.get(j).get(k).equals(a))
            {
                isMatch = true;
                break;
            }
            subSetString += subsets.get(j).get(k) + " ";
        }
        if(!isMatch) 
        {
            System.out.println(subSetString + " => " + a);
        }
    }
}
elevenights
  • 128
  • 1
  • 10