【字符串】C026_最常见的单词(map)

一、题目描述

Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn’t banned, and that the answer is unique.

Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.

Input: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"

二、题解

方法一:map

* 繁琐解法:出现的特例有很多,比如:

  • 字符串 paragraph 末尾没有 .,逗号 , 后面有空格。

这样处理起来会很麻烦。我们其实可以用 String 内部的 replace 方法,将干扰字符全部替换为空格 ' '

public String mostCommonWord(String para, String[] banned) {
    Set<String> ban = new HashSet<>();
    for (String b : banned)   
        ban.add(b.toLowerCase());

    Map<String, Integer> map = new HashMap<>();
    int l = 0, r = 0, N = para.length();
    
    while (r < N) {
        if (!Character.isLetter(para.charAt(r))) {
            String sub = para.substring(l, r).toLowerCase();
            if (!ban.contains(sub)) {
                map.put(sub, map.getOrDefault(sub, 0) + 1);
            }
            r++; l = r;
            while (r < N && !Character.isLetter(para.charAt(r))) {
                r++;
            }
            l=r;
        }
        r++;
    }
    String res = "";
    int max = -1;
    for (String key : map.keySet()) {
        if (map.get(key) > max) {
            res = key;
            max = map.get(key);
        }
    }
    return res;
}

* 更正后的代码:,坑超多的:

  • 将所有非字母的字符替换成空格。
    • 每次替换,需要都会产生一个新字符串,所以要重新对引用赋值。
  • 替换后,可能会出现空格字符串,所以要判断长度是否大于 0.
public String mostCommonWord(String para, String[] banned) {
    Map<String, Integer> map = new HashMap<>();
    Set<String> ban = new HashSet<>(Arrays.asList(banned));

    for (char c : para.toCharArray()) {
        if (!Character.isLetter(c))
            para = para.replace(c, ' ');
    }
    para = para.toLowerCase();
    String[] words = para.split(" ");

    for (String word : words) {
        if (word.length() > 0 && !ban.contains(word))
            map.put(word, map.getOrDefault(word, 0) + 1);
    }
    int max = -1;
    String res = "";
    for (String word : map.keySet()) {
        if (map.get(word) > max) {
            max = map.get(word);
            res = word;
        }
    }
    return res;
}

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)

方法二:


复杂度分析

  • 时间复杂度: O ( ) O() O()
  • 空间复杂度: O ( ) O() O()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值