当前位置:网站首页>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
边栏推荐
- Deep q-network (dqn)
- Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
- How to deploy a website with only a server and no domain name?
- How to write the expected salary on your resume to double your salary during the interview?
- Opencv reads webcam video and saves it locally
- 最通俗易懂的依赖注入之服务容器与作用域
- HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
- 7-11 rearrange the linked list (25 points)
- Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
- Q-Learning & Sarsa
猜你喜欢

Plug in for vscode

Laravel8- use JWT

Guangcheng cloud service can fill in a daily report regularly every day

REINFORCE

Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system

AspNetCore配置多环境log4net配置文件

Laravel new route file

全网最全,接口自动化测试怎么做的?精通接口自动化测试详解

Distributed system services
随机推荐
Openfeign details show
Basic workflow of CPU
Traversée de l'arbre L2 - 006
Er and eer models
Guangcheng cloud service can fill in a daily report regularly every day
Sonic cloud real machine tutorial
Configuring Apache Web services for servers such as Tianyi cloud
建立与遍历二叉树
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
Chapter VI project information management system summary
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Publish to NPM?
Chapter VII project communication management of information system project manager summary
JS learning notes
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
Dynamic sequence table + OJ
7-11 rearrange the linked list (25 points)
Processes and threads
Summary of interface automation interview questions for software testing