当前位置: 面试刷题>> 频率最高的词 (经典算法题500道)


完整题目描述

题目: 编写一个程序,用于找出给定文本(字符串)中频率最高的词(忽略大小写,且假设文本中的标点符号和空格已经被适当处理,只保留字母和空格),并返回这个词及其出现次数。如果有多个词出现次数相同且都是最高的,则返回其中任意一个即可。

示例输入

"Hello world, this is a test. Hello again, test test."

示例输出

"test", 3

PHP 示例代码

<?php

function findMostFrequentWord($text) {
    // 将文本转换为小写并去除非字母字符
    $text = preg_replace('/[^a-zA-Z\s]/', '', strtolower($text));
    // 分割成单词数组
    $words = explode(' ', $text);
    
    // 初始化频率数组
    $frequency = array();
    
    // 计算每个单词的频率
    foreach ($words as $word) {
        if (!empty($word)) { // 忽略空字符串
            if (!isset($frequency[$word])) {
                $frequency[$word] = 1;
            } else {
                $frequency[$word]++;
            }
        }
    }
    
    // 找到频率最高的词
    $maxFreq = 0;
    $mostFrequentWord = '';
    foreach ($frequency as $word => $freq) {
        if ($freq > $maxFreq) {
            $maxFreq = $freq;
            $mostFrequentWord = $word;
        }
    }
    
    return array($mostFrequentWord, $maxFreq);
}

// 示例用法
$text = "Hello world, this is a test. Hello again, test test.";
list($word, $freq) = findMostFrequentWord($text);
echo '"' . $word . '", ' . $freq;

?>

Python 示例代码

def find_most_frequent_word(text):
    # 将文本转换为小写并分割成单词列表,忽略非字母字符
    words = re.findall(r'\b\w+\b', text.lower())
    
    # 使用字典统计每个单词的频率
    frequency = {}
    for word in words:
        if word not in frequency:
            frequency[word] = 1
        else:
            frequency[word] += 1
    
    # 找到频率最高的词
    max_freq = max(frequency.values())
    most_frequent_word = [word for word, freq in frequency.items() if freq == max_freq][0]
    
    return most_frequent_word, max_freq

# 示例用法
import re
text = "Hello world, this is a test. Hello again, test test."
word, freq = find_most_frequent_word(text)
print(f'"{word}", {freq}')

JavaScript 示例代码

function findMostFrequentWord(text) {
    // 将文本转换为小写并去除非字母字符
    const cleanedText = text.toLowerCase().replace(/[^a-z\s]/gi, '');
    // 分割成单词数组
    const words = cleanedText.split(/\s+/);
    
    // 初始化频率对象
    const frequency = {};
    
    // 计算每个单词的频率
    words.forEach(word => {
        if (word) { // 忽略空字符串
            if (!frequency[word]) {
                frequency[word] = 1;
            } else {
                frequency[word]++;
            }
        }
    });
    
    // 找到频率最高的词
    let maxFreq = 0;
    let mostFrequentWord = '';
    Object.keys(frequency).forEach(word => {
        if (frequency[word] > maxFreq) {
            maxFreq = frequency[word];
            mostFrequentWord = word;
        }
    });
    
    return [mostFrequentWord, maxFreq];
}

// 示例用法
const text = "Hello world, this is a test. Hello again, test test.";
const [word, freq] = findMostFrequentWord(text);
console.log(`"${word}", ${freq}`);

码小课网站中有更多相关内容分享给大家学习,包括但不限于算法基础、数据结构、编程语言进阶等内容,欢迎大家访问学习。

推荐面试题