当前位置:网站首页>使用split来解决“最常见的单词”问题
使用split来解决“最常见的单词”问题
2022-04-23 03:02:00 【&小小白&】
十五、最常见的单词
15.1、题设要求
给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。
题目保证至少有一个词不在禁用列表中,而且答案唯一。
禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母。
示例:
输入:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
输出: "ball"
解释:
"hit" 出现了3次,但它是一个禁用的单词。
"ball" 出现了2次 (同时没有其他单词出现2次),所以它是段落里出现次数最多的,且不在禁用列表中的单词。
注意,所有这些单词在段落里不区分大小写,标点符号需要忽略(即使是紧挨着单词也忽略, 比如 "ball,"),
"hit"不是最终的答案,虽然它出现次数更多,但它在禁用单词列表中。
提示:
1 <= 段落长度 <= 1000
0 <= 禁用单词个数 <= 100
1 <= 禁用单词长度 <= 10
答案是唯一的, 且都是小写字母 (即使在 paragraph 里是大写的,即使是一些特定的名词,答案都是小写的。)
paragraph 只包含字母、空格和下列标点符号!?',;.
不存在没有连字符或者带有连字符的单词。
单词里只包含字母,不会出现省略号或者其他标点符号。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/most-common-word
15.2、解题思路
先将字符串中的符号去掉,再统计字符串中的单词出现的次数,然后将禁用的单词去掉,将去掉禁用单词后的map出现次数的单词输出即可。
15.3、算法
//将字符串中的符号去掉
String [] str = paragraph.split(" |\\!|\\?|'|\\;|\\.|,");
Map<String,Integer> map = new HashMap<>();
//统计字符串中的单词出现的次数
for (int i = 0; i < str.length; i++) {
//将数组b中的空格去掉
if (str[i].length()==0){
continue;
}
//将字母转换成小写字母
str[i] = str[i].toLowerCase();
//给map赋值,str[i]为map中的key,value则为依次递增
//getOrDefault()函数的作用是在map中得到传入key的value,如果得不到也就是value如果为空,则返回传入的参数的值
//在这里的话,如果value为空,初始的value则为1;如果value不为空,则返回key相对应的value再加一
map.put(str[i],map.getOrDefault(str[i],0)+1);
}
//将ban中禁用的单词在map中去掉
for(int i=0;i<banned.length;i++){
map.remove(banned[i]);
}
String ans=" ";
//取出map中的数比较并替换
for(String s:map.keySet()){
//将map中字符串s的value与getOrDefault(比较成功,递增)进行比较
if(map.get(s)>map.getOrDefault(ans,0)){
ans=s;
}
}
return ans;
版权声明
本文为[&小小白&]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_52916408/article/details/124240924
边栏推荐
- c#语法糖模式匹配【switch 表达式】
- The space between the left and right of the movie ticket seats is empty and cannot be selected
- 最通俗易懂的依赖注入之服务容器与作用域
- eventBus
- Er and eer models
- .NET点滴:说说Middleware构造中获取不到Scoped服务的问题
- Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
- Niuke white moon race 5 [problem solving mathematics field]
- Distributed system services
- C# 读写二进制文件
猜你喜欢

Detailed explanation of distributed things
![Niuke white moon race 5 [problem solving mathematics field]](/img/be/ca059bd1c84eaaaefa3266f9119a6b.png)
Niuke white moon race 5 [problem solving mathematics field]

Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel

微软是如何解决 PC 端程序多开问题的——内部实现

.NET点滴:说说Middleware构造中获取不到Scoped服务的问题

Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)

Processes and threads

Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

TP5 inherits base and uses the variables in base
随机推荐
Deep q-network (dqn)
C# 11 对 ref 和 struct 的改进
Use of MySQL command line client and common commands
C# WPF UI框架MahApps切换主题
JS using the parameters of art template
Systemctl start Prometheus + grafana environment
JS learning notes
利用栈的回溯来解决“文件的最长绝对路径”问题
ASP.NET 6 中间件系列 - 执行顺序
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
Kubernetes study notes
MYSQL_ From mastery to abandonment
How to write the expected salary on your resume to double your salary during the interview?
TP5 customization in extend directory succeeded and failed. Return information
Chapter VII project communication management of information system project manager summary
TP5 email (2020-05-27)
MAUI初体验:爽
Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
Redis data server / database / cache (2022)