当前位置:网站首页>648. Word replacement
648. Word replacement
2022-07-07 23:25:00 【PI Qiliang】
648. Word substitution 【 Medium question 】【 A daily topic 】
Ideas :
Sort the word roots by length
Traverse each word , For the current word , Traverse the root list , Filter out the prefix of the first word in the root , And update the current word with this prefix .
Finally, all the updated words are separated by spaces and spelled into strings to return .
Code :
class Solution {
public String replaceWords(List<String> dictionary, String sentence) {
// Sort root dictionaries by length
dictionary.sort(Comparator.comparingInt(String::length));
// Divide sentences into spaces
String[] words = sentence.split(" ");
int n = words.length;
StringBuilder ans = new StringBuilder();
for (int i = 0; i < n; i++) {
// Traverse each word in the word array
// Record the length of the current word
int word_n = words[i].length();
// Traverse the root dictionary , Filter out the first prefix of the current word and update it to the current position of the word array
for (String prefix : dictionary) {
// Record the current root length
int pre_n = prefix.length();
// If the root length is greater than the word , Then it cannot be a prefix , Because the root list is sorted from small to large according to the length , Therefore, the current root length is greater than the word , Then there is no need to continue the cycle , The length of the trailing root is greater than that of the word , Therefore, the current word has no prefix in the root dictionary , Words remain the same
// If the root length is equal to the word , If the root word is equal to the word , Then update the word to root , Equivalent to constant ; If the root word is not equal to the word , Then the words remain the same
// Sum up , Root length >= Word length , Words remain the same , It can be understood as There is no prefix in the root dictionary
if (pre_n >= word_n){
break;
}
// At this time, the root length must be less than the word length , If the root word is the prefix of the word , Then update the word with a prefix , Because the title requires to replace with the shortest root , So the loop exits
if (words[i].startsWith(prefix)){
words[i] = prefix;
break;
}
// If the root is not the prefix of the word , Then continue to cycle through the next root
}
// The processed words Append to ans Back , And append spaces
ans.append(words[i]).append(" ");
}
// take ans Turn it into a string and remove the trailing space, and then return
return ans.toString().trim();
}
}
边栏推荐
- 三问TDM
- The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
- Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
- Cloud native is devouring everything. How should developers deal with it?
- HDU 4747 mex "recommended collection"
- USB (十七)2022-04-15
- ArcGIS: two methods of attribute fusion of the same field of vector elements
- Network security -beef
- Unity3D学习笔记5——创建子Mesh
- PHP uses Alibaba cloud storage
猜你喜欢
Talk about the design and implementation logic of payment process
USB(十五)2022-04-14
伸展树(一) - 图文解析与C语言实现
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
Add data analysis tools in Excel
2022第六季完美童模陕西总决赛圆满落幕
七月第一周
Explain
UE4_UE5结合罗技手柄(F710)使用记录
Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
随机推荐
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
[compilation principle] lexical analysis design and Implementation
USB(十四)2022-04-12
Illegal behavior analysis 1
Network security - information query of operating system
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
Cloud native is devouring everything. How should developers deal with it?
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
Explain
在软件工程领域,搞科研的这十年!
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
Wechat forum exchange applet system graduation design (5) assignment
Wechat forum exchange applet system graduation design completion (1) development outline
Specific method example of V20 frequency converter manual automatic switching (local remote switching)
In the field of software engineering, we have been doing scientific research for ten years!
经纬度PLT文件格式说明
Unity3D学习笔记6——GPU实例化(1)
PCB wiring rules of PCI Express interface
Kubernetes' simplified data storage storageclass (creation, deletion and initial use)