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