【Brush questions diary】 The most common words

Get into the habit of writing together! This is the 16th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the event details .

【Brush questions diary】 The most common words

The 36th article of this writing diary, the title is: the most common words , simple

1. Topic description:

The most common word topic, at first glance, we thought we were looking for words with a frequency within a certain range in the vast dictionary. Looking closely, it doesn't seem to be so complicated, and it still looks very simple and easy to understand.

2. What idea does this question examine? What is your thinking?

Take a look at what important information the topic tells us:

  • The title gives a paragraph, this paragraph may contain multiple words, or it may contain one word, the word has uppercase and lowercase
  • The title also gives a list of forbidden words, which is in lowercase. We need to find the words that appear most frequently in the above paragraphs, and the words that are not in the forbidden list. Here we need to pay attention to the comparison and verification . Convert original uppercase letters to lowercase letters

The meaning of the question is very clear. When we see the topic of calculating the frequency of words, we may actually have a conditional reflex. This kind of topic has a high probability of using a hash table.

After careful analysis, we need to do two things:

  • Find each word explicitly and count how often each word occurs

It is easier to find words. We can use a temporary slice to store the traversed letters when traversing the paragraph string. When we check that the current character is a space or other non-letter character, we can determine that the temporary The number in the slice, already a complete word

Calculate the frequency of word occurrence, and put the above found words into the hash table to record the number

  • How to find the word with the largest number of occurrences, it can't be disabled yet

通过上述的遍历,和哈希表的处理,我们就可以用一个变量来存储出现频次最大的数字,后续遍历 哈希表的时候,就可以拿出来进行比对,找到对应的字符串,我们就可以实现这个题的要求了

上述思路还是比较清晰的,虽然没有图,但是文字已经能够简单的表达出思路了,接下来就开始撸代码吧

三、编码

根据上述逻辑和分析,我们就可以翻译成如下代码,此处的编码需要注意的是,咱们遍历段落循环的控制,我们可以根据判断当前字符不是字母的情况下,来校验当前是否已经找到一个单词

但是,我们需要注意的是,如果段落中,只有一个单词的时候,我们需要处理好这种场景

例如,我们可以遍历的时候,控制可以让索引等于段落字符串的长度,具体带编码细节

func mostCommonWord(paragraph string, banned []string) string {
    // 记录 banded 的字符
    bm := map[string]bool{}
    for _, b := range banned {
        bm[b] = true
    }
    // 记录段落的长度
    length := len(paragraph)
    // 定义记录单词评率的 map
    helper := map[string]int{}
    var tmp []byte
    var maxFreq int
    // 此处控制的循环就可以是 等于 length 的, 原因是,当给出的段落中就只有一个单词的时候,我们也是要进行处理的
    for i:=0; i<=length; i++ {
        // 判断如果是字母,则咱们继续将该字母加入到 tmp 切片中
        if  i<length && unicode.IsLetter(rune(paragraph[i])){
            tmp = append(tmp,byte(unicode.ToLower(rune(paragraph[i]))))
        }else if tmp != nil{
            // 不是字母的时候,说明这个时候已经是空格了,那么 tmp 已经是一个单词了,此时就要处理改单词出现频率的事情了
            // 还需要查看该字符串是否被禁用了
            s := string(tmp)
            if !bm[s]{
                helper[s]++
                maxFreq = max(maxFreq,helper[s])
            }
            tmp = nil
        }
    }

    for s,v := range helper{
        if maxFreq == v{
            return s
        }
    }
    return ""
}
// 写一个比较数字的函数
func max(a,b int) int {
    if a>b {
        return a
    }
    return b
}
复制代码

咱们编码的思路可以理解是这样的

  • 先将被禁用的单词,用一个 map 来记录一波
  • 遍历段落字符串,计算出对应单词出现的频率,存放到 helper 哈希表中,和记录最大频率的数值
  • 遍历 helper 哈希表,判断未被禁用的字符串出现的频率是否等于目前记录的最大频率,若是,则返回该字符串

四、总结:

这种实现方式的时间复杂度是多少呢,我们可以看到,题目中的实现,我们要是对于给出的禁用列表做了一次遍历,对于题目给出的段落字符串进行了一次遍历,那么咱们的时间复杂是 O(m+n)

The same is true for the space complexity. We introduce 2 maps, resulting in space consumption. Then our space complexity is also O(m+n) , where m represents the length of the disabled list, and n represents the length of the paragraph string.

Original title address: 819. The most common words

I am here today, what I have learned, if there are any deviations, please correct me

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~

Guess you like

Origin juejin.im/post/7087944076183470087