返回介绍

solution / 0800-0899 / 0819.Most Common Word / README_EN

发布于 2024-06-17 01:03:34 字数 6159 浏览 0 评论 0 收藏 0

819. Most Common Word

中文文档

Description

Given a string paragraph and a string array of the banned words banned, return _the most frequent word that is not banned_. It is guaranteed there is at least one word that is not banned, and that the answer is unique.

The words in paragraph are case-insensitive and the answer should be returned in lowercase.

 

Example 1:

Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation: 
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"), 
and that "hit" isn't the answer even though it occurs more because it is banned.

Example 2:

Input: paragraph = "a.", banned = []
Output: "a"

 

Constraints:

  • 1 <= paragraph.length <= 1000
  • paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".
  • 0 <= banned.length <= 100
  • 1 <= banned[i].length <= 10
  • banned[i] consists of only lowercase English letters.

Solutions

Solution 1

class Solution:
  def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
    s = set(banned)
    p = Counter(re.findall('[a-z]+', paragraph.lower()))
    return next(word for word, _ in p.most_common() if word not in s)
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Solution {
  private static Pattern pattern = Pattern.compile("[a-z]+");

  public String mostCommonWord(String paragraph, String[] banned) {
    Set<String> bannedWords = new HashSet<>();
    for (String word : banned) {
      bannedWords.add(word);
    }
    Map<String, Integer> counter = new HashMap<>();
    Matcher matcher = pattern.matcher(paragraph.toLowerCase());
    while (matcher.find()) {
      String word = matcher.group();
      if (bannedWords.contains(word)) {
        continue;
      }
      counter.put(word, counter.getOrDefault(word, 0) + 1);
    }
    int max = Integer.MIN_VALUE;
    String ans = null;
    for (Map.Entry<String, Integer> entry : counter.entrySet()) {
      if (entry.getValue() > max) {
        max = entry.getValue();
        ans = entry.getKey();
      }
    }
    return ans;
  }
}
class Solution {
public:
  string mostCommonWord(string paragraph, vector<string>& banned) {
    unordered_set<string> s(banned.begin(), banned.end());
    unordered_map<string, int> counter;
    string ans;
    for (int i = 0, mx = 0, n = paragraph.size(); i < n;) {
      if (!isalpha(paragraph[i]) && (++i > 0)) continue;
      int j = i;
      string word;
      while (j < n && isalpha(paragraph[j])) {
        word.push_back(tolower(paragraph[j]));
        ++j;
      }
      i = j + 1;
      if (s.count(word)) continue;
      ++counter[word];
      if (counter[word] > mx) {
        ans = word;
        mx = counter[word];
      }
    }
    return ans;
  }
};
func mostCommonWord(paragraph string, banned []string) string {
  s := make(map[string]bool)
  for _, w := range banned {
    s[w] = true
  }
  counter := make(map[string]int)
  var ans string
  for i, mx, n := 0, 0, len(paragraph); i < n; {
    if !unicode.IsLetter(rune(paragraph[i])) {
      i++
      continue
    }
    j := i
    var word []byte
    for j < n && unicode.IsLetter(rune(paragraph[j])) {
      word = append(word, byte(unicode.ToLower(rune(paragraph[j]))))
      j++
    }
    i = j + 1
    t := string(word)
    if s[t] {
      continue
    }
    counter[t]++
    if counter[t] > mx {
      ans = t
      mx = counter[t]
    }
  }
  return ans
}
function mostCommonWord(paragraph: string, banned: string[]): string {
  const s = paragraph.toLocaleLowerCase();
  const map = new Map<string, number>();
  const set = new Set<string>(banned);
  for (const word of s.split(/[^A-z]/)) {
    if (word === '' || set.has(word)) {
      continue;
    }
    map.set(word, (map.get(word) ?? 0) + 1);
  }
  return [...map.entries()].reduce((r, v) => (v[1] > r[1] ? v : r), ['', 0])[0];
}
use std::collections::{ HashMap, HashSet };
impl Solution {
  pub fn most_common_word(mut paragraph: String, banned: Vec<String>) -> String {
    paragraph.make_ascii_lowercase();
    let banned: HashSet<&str> = banned.iter().map(String::as_str).collect();
    let mut map = HashMap::new();
    for word in paragraph.split(|c| !matches!(c, 'a'..='z')) {
      if word.is_empty() || banned.contains(word) {
        continue;
      }
      let val = map.get(&word).unwrap_or(&0) + 1;
      map.insert(word, val);
    }
    map.into_iter()
      .max_by_key(|&(_, v)| v)
      .unwrap()
      .0.to_string()
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文