A simple solution in java


#1

public class Solution {
public ArrayList solve(String A, ArrayList B) {
String arr[] = A.split("_");
HashSet set = new HashSet();
for(int i=0; i<arr.length ; i++)
{
set.add(arr[i]);
}

    ArrayList<GoodReview> al = new ArrayList<GoodReview>();
    
   
    for(int i=0; i<B.size();i++)
    {
        
            int good = getGoodnessValue(B.get(i),set);
            GoodReview gr = new GoodReview(B.get(i),good,i);
            al.add(gr);
    }
    
    Collections.sort(al);
     ArrayList<Integer> ans = new ArrayList<Integer>();
     for(GoodReview g : al)
     {
         ans.add(g.index);
     }
     
    // Collections.reverse(ans);
     return ans;
}

public int getGoodnessValue(String s , HashSet<String> set)
{
    String arr[] = s.split("_");
    int count = 0;
    for(int i=0 ;i<arr.length ;i++)
    {
        if(set.contains(arr[i]))
        count++;
    }
    
    return count;
}

}

class GoodReview implements Comparable
{
String s;
int goodness;
int index;

public GoodReview(String s , int goodness , int index)
{
    this.s = s;
    this.goodness = goodness;
    this.index = index;
}

public int compareTo(GoodReview g)
{
    if(goodness==g.goodness){
        
        if(index==g.index)
        return 0;
        else if(index>g.index)
        return 1;
        else
        return -1;
        
    }    
    else if(goodness<g.goodness)    
    return 1;    
    else    
    return -1;  
}

}


#2

Why there is a need to store String s in class GoodReview implements Comparable ??

I guess int goodness and index are enough to find the answer .