查找字符串中重复次数最多的单词

时间:2018-11-16 12:22:15

标签: java

关于如何在字符串中找到最常见的单词,您能给我一些指导吗?我无法使用地图,列表等。我只能通过for和if以及一些内置方法来实现这一点。

4 个答案:

答案 0 :(得分:1)

将字符串拆分并保存到数组,对数组进行排序,遍历排序后的数组,并对相同字符串的计数频率进行更新以更新最大计数。示例:

public static void main(String[] args) {
    String myStr = "how can I find the most frequent word in an string how can I find how how how string";
    String[] splited = myStr.split(" ");
    Arrays.sort(splited);
    System.out.println(Arrays.toString(splited));
    int max = 0;
    int count= 1;
    String word = splited[0];
    String curr = splited[0];
    for(int i = 1; i<splited.length; i++){
        if(splited[i].equals(curr)){
            count++;
        }
        else{
            count =1;
            curr = splited[i];
        }
        if(max<count){
            max = count;
            word = splited[i];
        }
    }
    System.out.println(max + " x " + word);
}  

答案 1 :(得分:0)

public static void main(String...strings) {
        String para = "Paris in the the spring.Not that that is related.Why are you laughing? Are my my regular expressions THAT bad??";
        String[] words = para.split("\\s+");
        int finalCount = 0;
        int tempCount = 0;
        String mostlyUsedWord = null;
        for (String word: words) {
            tempCount = 0;
            for (String w: words) {
                if (word.equalsIgnoreCase(w)) {
                    tempCount++;
                }
            }
            if (tempCount >= finalCount) {
                finalCount = tempCount;
                mostlyUsedWord = word;
            }
        }
        System.out.println("mostlyUsedWord:: = " + mostlyUsedWord + " ,count:: = " + finalCount);
    }

答案 2 :(得分:0)

示例想法(有千种方法可以解决此问题)

1:A B B C B(<带单词的字符串,用空格分隔)    “ A”是您的开始位置

2:计算A(1)并保存A(0)的位置。您总是从pos迭代到String的结尾。

3:继续计数,直到遍历整个String。当您到达字符串的末尾时,请将计数分配给另一个变量(例如oldCount)来保存计数。

4:移至下一个单词并开始计数B(新位置= 1)。您将要数3B。如果新计数>旧计数替换旧计数。

5:计算下一个单词,并将位置更新为当前位置,即3(字符串的最后一个位置)。

6:您不会更新计数器,B是字符串中使用最多的单词。

答案 3 :(得分:0)

对于纯粹主义者-只需循环和String

private String mostFrequentWord(String words) {
    // Where my current word starts.
    int wordStart = 0;
    // How many I counted.
    int wordCount = 0;
    // The currently most frequent.
    String word = "";
    for (int wordEnd = wordStart; wordEnd < words.length(); wordEnd++) {
        // Is this the end of a word?
        if (wordEnd > words.length() || words.charAt(wordEnd) == ' ') {
            // We have a word! How many times does it occur?
            String thisWord = words.substring(wordStart, wordEnd);
            // How many times this word occurs.
            int thisWordCount = 0;
            // Current start of search.
            int search = -1;
            // Count them.
            while ((search = words.indexOf(thisWord, search + 1)) >= 0) {
                thisWordCount += 1;
            }
            // Is it longer?
            if (thisWordCount > wordCount) {
                // Keep track.
                word = thisWord;
                wordCount = thisWordCount;
            }
            // Move start to the next word.
            wordStart = wordEnd + 1;
        }
    }
    return word;
}

private void test() {
    String words = "Now is the time for all good men to come to the aid of the party";
    System.out.println("Most frequent word in \"" + words + "\" is " + mostFrequentWord(words));
}