当前位置:网站首页>Use split to solve the "most common words" problem
Use split to solve the "most common words" problem
2022-04-23 03:05:00 【& Xiaobai &】
15、 ... and 、 The most common word
15.1、 Question design requirements
Given a paragraph (paragraph) And a list of forbidden words (banned). Return the most times , Words that are not in the disable list at the same time .
The title guarantees that at least one word is not in the forbidden list , And the only answer is .
Words in the disable list are represented in lowercase letters , No punctuation . Words in paragraphs are not case sensitive . The answers are in small letters .
Example :
Input :
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output : "ball"
explain :
"hit" There is 3 Time , But it's a forbidden word .
"ball" There is 2 Time ( At the same time, no other words appear 2 Time ), So it's the most frequent one in the paragraph , Words that are not in the disable list .
Be careful , All these words are case insensitive in paragraphs , Punctuation needs to be ignored ( Even if it's next to a word, ignore , such as "ball,"),
"hit" Not the final answer , Although it appears more often , But it's in the list of forbidden words .
Tips :
1 <= Paragraph length <= 1000
0 <= Disable several words <= 100
1 <= Disable word length <= 10
The answer is only , And they're all small letters ( Even in paragraph It's in capitals , Even some specific nouns , The answers are in lowercase .)
paragraph Letters only 、 Spaces and the following punctuation !?',;.
There are no words without or with hyphens .
Words contain only letters , There will be no ellipsis or other punctuation marks .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/most-common-word
15.2、 Their thinking
First remove the symbol from the string , Then count the number of words in the string , Then remove the forbidden words , Remove the words after the forbidden words map The word output of the number of occurrences can .
15.3、 Algorithm
// Remove the symbol from the string
String [] str = paragraph.split(" |\\!|\\?|'|\\;|\\.|,");
Map<String,Integer> map = new HashMap<>();
// Count the number of occurrences of words in the string
for (int i = 0; i < str.length; i++) {
// Will array b Remove the space in the
if (str[i].length()==0){
continue;
}
// Convert letters to lowercase letters
str[i] = str[i].toLowerCase();
// to map assignment ,str[i] by map Medium key,value Then it increases in order
//getOrDefault() The function is used in map Get passed in from key Of value, If not, that is value If it is empty , Returns the value of the passed in parameter
// In this case , If value It's empty , Initial value Then for 1; If value Not empty , Then return to key Corresponding value One more
map.put(str[i],map.getOrDefault(str[i],0)+1);
}
// take ban Disabled words in map Removing the
for(int i=0;i<banned.length;i++){
map.remove(banned[i]);
}
String ans=" ";
// Take out map Compare and replace the numbers in
for(String s:map.keySet()){
// take map Middle string s Of value And getOrDefault( Quite successful , Increasing ) Compare
if(map.get(s)>map.getOrDefault(ans,0)){
ans=s;
}
}
return ans;
版权声明
本文为[& Xiaobai &]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301476720.html
边栏推荐
- 7-11 重排链表 (25 分)
- Close the computer port
- MYSQL05_ Ordr by sorting, limit grouping, group by grouping
- Array and collection types passed by openfeign parameters
- Restart redis
- MYSQL_ From mastery to abandonment
- Chapter VI project information management system summary
- Typescript Learning Guide
- Guangcheng cloud service can fill in a daily report regularly every day
- C# WPF UI框架MahApps切换主题
猜你喜欢
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
How to use C language to realize [guessing numbers game]
Encapsulation of ele table
最通俗易懂的依赖注入与控制反转
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Openfeign timeout setting
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Summary of software test interview questions
微软是如何解决 PC 端程序多开问题的
准备一个月去参加ACM,是一种什么体验?
随机推荐
Basic SQL (VIII) data update operation practice
JS learning notes
How to deploy a website with only a server and no domain name?
Restart redis
[Euler plan question 13] sum of large numbers
Load view Caton
TP5 inherits base and uses the variables in base
Plug in for vscode
Chapter IV project cost management of information system project manager summary
微软是如何解决 PC 端程序多开问题的
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
tf. keras. layers. Embedding function
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
Service avalanche effect
Introduction to ACM [TSP problem]
Processes and threads
JS relearning
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
Chapter VII project communication management of information system project manager summary
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang