2011-01-10 111 views
1

我试图找到一个字符串中重复最多的一句话,与此代码:操作字符串

public class Word 
{ 
    private String toWord; 
    private int Count; 

    public Word(int count, String word){ 
     toWord = word; 
     Count = count; 
    } 

    public static void main(String args[]){ 
     String str="my name is neo and my other name is also neo because I am neo"; 
     String []str1=str.split(" "); 
     Word w1=new Word(0,str1[0]); 
     LinkedList<Word> list = new LinkedList<Word>(); 
     list.add(w1); 
     ListIterator itr = list.listIterator(); 
     for(int i=1;i<str1.length;i++){ 
      while(itr.hasNext()){ 
       if(str1[i].equalsTO(????)); 
       else 
        list.add(new Word(0,str1[i])); 
      } 
     } 
    } 
} 

如何比较字符串从字符串数组str1存储在链接的列表中,然后字符串我该如何增加相应的计数。

然后我会打印最高的字符串, 我不知道该怎么做。

+0

您使用哪种语言?格式化您的代码。这是功课吗? – 2011-01-10 01:12:03

+0

这是功课吗?你有什么尝试?为什么它不工作?更加详细一些。 – templatetypedef 2011-01-10 01:12:51

+0

我想比较输入字符串与链接列表中的一个,如果它匹配我增加字符串的计数,我的问题是我如何与链接列表中的字符串比较 – NEO 2011-01-10 01:23:25

回答

0

您需要将每个单词存储到一个列表中,也许是一个长的计数变量,表示该单词已被使用了多少次。

对于每个单词,如果它已经在列表中,则增加计数;如果不是,则将其添加到列表中。

+0

Ya我知道算法,但我如何比较字符串是否在链接列表中,请查看我的代码 – NEO 2011-01-10 01:17:24

+0

将字符串中的每个单词添加到数组的方式对您不利,因为它可能包含重复项。正如我所提到的,逐个添加每个单词到列表中。如果该单词已经在列表中,则增加计数而不是再次添加。 – 2011-01-10 01:20:25

0

C#?您可以尝试使用LINQ GroupBy,然后使用Count或Max - 非常简单。

0

使用Google Guava

Multiset<String> words = HashMultiset.create(Splitter.on(" ").split(input)); 

然后

String topWord = words.isEmpty() ? null 
    : Iterables.get(Ordering.natural().immutableSortedCopy(words), 0); 

你可以得到顶级词的出现次数与words.count(topWord)

4

我会建议使用HashMap而不是链接列表。

Iterate through the string.
For each word,
Check if the word is in the Map,
If it is there increment count and
Otherwise insert with count 1

0

我想你可以使用一些正则表达式在这里像

final String str = "my name is neo and my other name is also neo because I am neo"; 

    final String[] arr = str.split (" "); 
    final Set <String> set = new HashSet <String>(); 
    for (final String word : arr) { 
     System.out.println ("arr " + word); 
     set.add (word); 
    } 

    String preWord = ""; 
    int preCount = 0; 
    for (final String word : set) { 
     System.out.println ("----------------"); 

     final Pattern p2 = Pattern.compile ("\\b" + word + "\\b"); 
     final Matcher m2 = p2.matcher (str); 
     int count = 0; 

     while (m2.find()) { 
      count++; 
     } 

     System.out.println ("preCount " + preWord + ":" + word + ":" + preCount + ":" + count); 

     if ((preCount < count)) { 
      preWord = word; 
      preCount = count; 
      System.out.println ("assigning word " + word + ":" + count); 
     } 
    } 

    System.out.println ("result " + preWord + ":" + preCount); 
0

使用Apache下议院StringUtils的org.apache.commons.lang.StringUtils得到计数。

String str="my name is neo and my other name is also neo because I am neo"; 
// Make a unique list (java.util.Set) of words. 
Set<String> stSet = new HashSet<String>(Arrays.asList(str.split(" "))); 
int sz = stSet.size(); 
int[] counts = new int[sz]; 
Map<Integer,String> matches = new HashMap<Integer,String>(sz); 
int i = 0; 
for (String s : stSet) { 
    // saves the individual word count in a sortable array. 
    counts[i] = StringUtils.countMatches(str,s)); 
    // saves the word count and the word in a HashMap for easy retrieval. 
    matches.put(counts[i],s); 
    i++; 
} 
Arrays.sort(counts); 
int max = counts.length - 1; 
System.out.println("The the word with the most occurrances is: "+matches.get(counts[max])+", the number of occurrances is: "+counts[max]);