leetcode Most Common Word——就是在考察自己实现split
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.
Example:
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.
class Solution(object):
def split_word(self, paragraph):
sep = set("[!?',;.] ")
ans = []
pos = 0
for i,c in enumerate(paragraph):
if c in sep:
word = paragraph[pos:i]
if word:
ans.append(word)
pos = i+1
word = paragraph[pos:]
if word:
ans.append(word)
return ans def mostCommonWord(self, paragraph, banned):
"""
:type paragraph: str
:type banned: List[str]
:rtype: str
"""
m = {}
paragraph = paragraph.lower()
for w in self.split_word(paragraph):
if w in banned: continue
m[w] = m.get(w, 0)+1
ans = ""
max_cnt = 0
for w,c in m.items():
if c > max_cnt:
ans = w
max_cnt = c
return ans
使用字符串替换也可以实现,就是找到哪些字符应该直接remove掉,然后再分割:
public static String mostCommonWord(String paragraph, String[] banned) {
String[] splitArr = paragraph.replaceAll("[!?',;.]","").toLowerCase().split(" ");
HashMap<String, Integer> map = new HashMap<>();
List<String> bannedList = Arrays.asList(banned);
for(String str: splitArr) {
if(!bannedList.contains(str)) {
map.put(str, map.getOrDefault(str, 0) + 1);
}
}
int currentMax = 0;
String res = "";
for(String key: map.keySet()) {
res = map.get(key) > currentMax ? key : res;
currentMax = map.get(key);
}
return res;
}
还有使用内置python的正则表达式:
Python:
Thanks to @sirxudi I change one line from
words = re.sub(r'[^a-zA-Z]', ' ', p).lower().split()
to
words = re.findall(r'\w+', p.lower()) def mostCommonWord(self, p, banned):
ban = set(banned)
words = re.findall(r'\w+', p.lower())
return collections.Counter(w for w in words if w not in ban).most_common(1)[0][0]
leetcode Most Common Word——就是在考察自己实现split的更多相关文章
- [LeetCode] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- LeetCode – Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- LeetCode 819. Most Common Word (最常见的单词)
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- 【Leetcode_easy】819. Most Common Word
problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...
随机推荐
- 用 Anaconda 完美解决 Python2 和 python3 共存问题
Python3 被越来越多的开发者所接受,同时让人尴尬的是很多遗留的老系统依旧运行在 Python2 的环境中,因此有时你不得不同时在两个版本中进行开发,调试. 如何在系统中同时共存 Python2 ...
- React Native 异步存储
异步存储 http://blog.csdn.net/yulianlin/article/details/52473456
- Codeforces 729E Subordinates
题目链接:http://codeforces.com/problemset/problem/729/E 既然每一个人都有一个顶头上司,考虑一个问题: 如果这些人中具有上司数目最多的人有$x$个上司,那 ...
- P1622 释放囚犯
传送门 区间DP简介: 在写这题前,需要先弄清楚区间DP是如何操作的: 区间DP的做法还是相对固定的,没有其他类型DP的复杂多变.主要思想就是先在小区间进行DP得到最优解,然后再利用小区间的最优解合并 ...
- java进程占用系统内存高,排查解决
转自:http://blog.51cto.com/chengxiaobai/2052530?cid=695076 故障:最近收到生产服务器的报警短信以及邮件,报警内容为:内存使用率高于70%. 使用t ...
- jmeter学习四配置元件详解
JMeter提供的配置元件中的HTTP属性管理器用于尽可能模拟浏览器行为,在HTTP协议层上发送给被测应用的http请求 1.Http信息头管理器 用于定制Sampler发出的HTTP请求的请求头的内 ...
- SPOJ 刷题记录
按点赞数降序 297 二分 #include<bits/stdc++.h> using namespace std; #define fi first #define se second ...
- layui 3种导航栏
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html ...
- Use GDB to debug a C++ program called from a shell script
解决了我一个大问题!!! http://stackoverflow.com/questions/5048112/use-gdb-to-debug-a-c-program-called-from-a-s ...
- p2725 Stamps
背包. #include <iostream> #include <cstdio> #include <cmath> #include <algorithm& ...