Count the Number of String Matches

2019-06-14 10:55发布

问题:

I want to get the smallest match of strings in the list. Though I'm successful doing so but the problem is i want to count that how many match counts have been made so:

List<String> mylist=new LinkedList<String>();
Set<String> result=new LinkedHashSet<String>();
mylist.add("interpreter");
mylist.add("interprete");
mylist.add("interpret");

mylist.add("developed");
mylist.add("develops");
mylist.add("develop");
mylist.add("interpret");


String small="";
Collections.sort(mylist);
Collections.reverse(mylist);

for(int i=0;i<mylist.size();i++)
{

  small=mylist.get(i);

   for(int j=i;j<mylist.size();j++)
   {
    if(small.contains(mylist.get(j)))
    {
        small=mylist.get(j);
    }
   }
   result.add(small);
}
for (String string : result) {
   System.out.println(string);
}

So that the output should be:

interpret=4
develop=4

Problem occurs with the following code i am trying:

List<String> mylist=new LinkedList<String>();
Set<String> result=new LinkedHashSet<String>();
mylist.add("interpreter");
mylist.add("interprete");
mylist.add("interpret");

mylist.add("developed");
mylist.add("develops");
mylist.add("develop");
mylist.add("interpret");
mylist.add("crawler");
mylist.add("crawl");
mylist.add("mobile");
mylist.add("mob");
mylist.add("juni");
mylist.add("junis");

Collections.sort(mylist);
Collections.reverse(mylist);

String small="";
int c=0;

for(int i=0;i<mylist.size();i++)
{
    c+=1;
    small=mylist.get(i);
    for(int j=i;j<mylist.size();j++)
    {
        if(small.contains(mylist.get(j)))
            {
                small=mylist.get(j);
                c+=1;
            }
    }
    result.add(small);

}
for (String string : result) {
    System.out.println(string+"="+c);
}

can somebody help me please!

回答1:

Putting @jambriz's answer in your code:

1.Use a HashMap

HashMap<String, Integer> result= new LinkedHashMap<String, Integer>();

2.Instead of result.add(small); now, add the value in hashmap only if the value is new or the count is less than the previous count. Also, set c=0 here

if (!result.containsKey(small) || result.get(small) < c)
    result.put(small, c);
c = 0;

3.At Last print your results:

for (String key : result.keySet())
    System.out.println(key + ": " + result.get(key)); 


回答2:

ok, first of all, your first code would print

interpret
develop

because you aren´t counting anything and it should be

interpret=4
develop=3

anyway.

The second block only has one counter 'c'. You should have one counter per each found word. I would suggest using a Map of Strings and Integers. When the String is nonexistent you put(small,1) and when it exists you get the Integer and add one to it. Let us know if that worked

(btw, there's no regex in your code, it shouldn't be tagged as such)



回答3:

String[] smallestStrings; //this has "interpret", "develop"

int[] matches = new int[smallestStrings.length];

for (int i = 0; i < matches.length; i++) {
    matches[i] = 0;
    for (String s : mylist) if (s.contains(smallestStrings[i])) matches[i]++;
}