views:

115

answers:

2

I have following code in Java:

import java.util.*;
public class longest{
public static void main(String[] args){
int t=0;int m=0;int token1, token2;
   String words[]=new String[10];
    String word[]=new String[10];
String common[]=new String[10];
   String text="saqartvelo gabrwyindeba da gadzlierdeba aucileblad ";
     String text1="saqartvelo gamtliandeba da gadzlierdeba aucileblad";
StringTokenizer st=new StringTokenizer(text);
StringTokenizer st1=new StringTokenizer(text1);

token1=st.countTokens();
token2=st1.countTokens();
while (st.hasMoreTokens()){

words[t]=st.nextToken();
  t++;
}
  while (st1.hasMoreTokens()){
     word[m]=st1.nextToken();
  m++;
}
 for (int k=0;k<token1;k++){
     for (int f=0;f<token2;f++){
      if (words[f].compareTo(word[f])==0){
  common[f]=words[f];
}
}
}
   while (i<common.length){
   System.out.println(common[i]);
   i++;
}
}
}

I want that in common array put elements which i in both text or these words

  • saqartvelo (georgia in english)
  • da (and in english)
  • gadzlierdeba (will be stronger)
  • aucileblad (sure)

and then between these words find string which has maximum length but it does not work more correctly it show me these words and also many null elements.

How do I correct it?

+1  A: 

Instead of manually searching for common words, why not put each sentence's words into a Set and then compute the intersection of both sets using retainAll()?

This tutorial on the Set Interface may help.

I assume this is homework... have you learned about algorithmic complexity, aka Big-O notation? If so, consider the complexity of your posted code vs. using a TreeSet vs. using a HashSet.

Dan
Algorithmically, DP LCS is the way to go. http://en.wikipedia.org/wiki/Longest_common_substring
polygenelubricants
Maybe I misunderstood the OP, because to me it doesn't sound like LCS at all. I thought the goal was to find the word common to both sentences which contained the most characters. Wouldn't LCS return "ndeba da gadzlierdeba aucileblad", which is a partial-word and three full words?
Dan
it is not homework thank u polygenelubricants thanks very much everybody
+2  A: 

The following snippet should be instructive:

    import java.util.*;
    //...

    String text1 = "saqartvelo gabrwyindeba da gadzlierdeba aucileblad";
    String text2 = "saqartvelo gamtliandeba da gadzlierdeba aucileblad";

    List<String> common = new ArrayList<String>();
    for (String s1 : text1.split(" ")) {
        for (String s2 : text2.split(" ")) {
            if (s1.equals(s2)) {
                common.add(s1);
            }
        }
    }

    Collections.sort(common, new Comparator<String>() {
        @Override public int compare(String s1, String s2) {
            return s2.length() - s1.length();
        }       
    });

    System.out.println(common);
    // prints "[gadzlierdeba, saqartvelo, aucileblad, da]"

Key ideas:

Related questions


An alternative solution

Note that the above solution is O(N^2), since it check each pair of words to see if they're equal. This means that it doesn't scale well when the two texts have many words. Using a Set such as a HashSet, you can do this in expected O(N) time, using Set.retainAll to compute the intersection of two sets.

static Set<String> wordSet(String text) {
    return new HashSet<String>(Arrays.asList(text.split(" ")));
}
//...

String text1 = ...;
String text2 = ...;

Set<String> commonSet = wordSet(text1);
commonSet.retainAll(wordSet(text2));

List<String> common = new ArrayList<String>(commonSet);
System.out.println(common);
// prints "[da, aucileblad, saqartvelo, gadzlierdeba]"
// in no particular order

// sort by string length using Comparator as above
polygenelubricants