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

Java 查找字符串中重复次数最多的单词,java,Java,你能给我一些提示吗?我怎样才能找到字符串中最常用的单词?我不能使用地图、列表等。我应该只通过for和if以及一些内置方法来实现这一点。示例想法(有上千种方法可以解决此问题): 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 expres

你能给我一些提示吗?我怎样才能找到字符串中最常用的单词?我不能使用地图、列表等。我应该只通过for和if以及一些内置方法来实现这一点。

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

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);
    }
1:A:B(<带单词的字符串,用空格分隔) “A”是你的起始位置

2:计数A(1)并保存A(0)的位置。您总是从
pos
迭代到字符串的末尾

3:继续计数,直到迭代整个字符串。当到达字符串末尾时,通过将计数分配给另一个变量(例如oldCount)来保存计数

4:转到下一个单词并开始计算B(新位置=1)。你要数到3个B。如果较新计数>较旧计数,则替换较旧计数

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


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

拆分字符串并保存到数组,对数组进行排序,迭代排序后的数组,并对更新最大计数的相同字符串的计数频率进行计数。例如:

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);
}  
publicstaticvoidmain(字符串[]args){
String myStr=“我怎样才能找到字符串中最常用的单词我怎样才能找到how String”;
String[]splited=myStr.split(“”);
数组。排序(拆分);
System.out.println(Arrays.toString(splited));
int max=0;
整数计数=1;
字符串字=拆分的[0];
字符串curr=splited[0];

对于(int i=1;i对于纯粹主义者,只需循环和
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));
}
私有字符串mostFrequentWord(字符串字){
//我现在的单词从哪里开始。
int-wordStart=0;
//我数了多少。
int字数=0;
//这是目前最常见的。
字串=”;
for(int-wordEnd=wordStart;wordEndwords.length()| | words.charAt(wordEnd)=''){
//我们有一个词!它发生了多少次?
String thisWord=words.substring(wordStart,wordEnd);
//这个词出现了多少次。
int thisWordCount=0;
//当前开始搜索。
int search=-1;
//数一数。
而((search=words.indexOf(thisWord,search+1))>=0){
thisWordCount+=1;
}
//它更长吗?
如果(thisWordCount>wordCount){
//跟踪。
单词=这个单词;
wordCount=thisWordCount;
}
//将开始移动到下一个单词。
wordStart=wordEnd+1;
}
}
返回词;
}
专用无效测试(){
String words=“现在是所有好人都来帮助党的时候了”;
System.out.println(“在\”“单词+”中最频繁的单词是“+最频繁的单词(单词));
}

1)删除标点符号2)按空格拆分句子3)单词映射->频率4)迭代映射,获取具有最高value@Michael…我无法使用映射…也许您可以对数组进行排序(拆分字符串后)并计算相邻单词,根据单词更改重置,并查找最大repeats@Michael我不能使用地图。@Uponn啊,这是一个设计拙劣的作业,会对学生施加任意的限制,因为他们不能想出一个真正适合他们所教材料的练习。祝你好运!t他的遗嘱给出了“that”作为结果,搜索不区分大小写。我认为模式在这里有点过大。为什么不使用内部循环和字符串。equals?谢谢,它很可能是单词,但正如我所说的,我只能用于循环、if语句和一些内置方法(indexOf、compare、isEqual等).我们还没有学会Pattern和Matcher,所以我不知道他们现在在做什么。@Hades我选择了另一个答案作为最佳答案,因为我们还没有学会你使用的每一个答案。另一个答案对我来说也很容易理解发生了什么。@Uponn但是你明确提到不要使用列表,我没有,anyways任何适合您的选项。您可能可以移动
if(maxnothandel equal frequency)。如果有相同频率的字符串,则只返回第一个字符串。@Uponn这个答案如何是最好的??使用数组IE,这是不区分大小写的操作??尽管您说过不要使用数组或相关类?