当前位置:网站首页>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();
}
}
边栏推荐
- First week of July
- Unity3d learning notes 5 - create sub mesh
- Network security - phishing
- Install a new version of idea. Double click it to open it
- Wechat forum exchange applet system graduation design (3) background function
- LDO voltage stabilizing chip - internal block diagram and selection parameters
- 给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
- Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
- In the field of software engineering, we have been doing scientific research for ten years!
- Explain
猜你喜欢
LeeCode -- 6. Zigzag transformation
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
Spark 离线开发框架设计与实现
深入理解Mysql锁与事务隔离级别
Installing spss25
Matlab SEIR infectious disease model prediction
Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
Mysql索引优化实战二
LDO穩壓芯片-內部框圖及選型參數
Talk about the design and implementation logic of payment process
随机推荐
Talk about the design and implementation logic of payment process
Dynamics 365 查找字段过滤
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
Network security - information query of operating system
Explain
Network security -beef
Network security sqlmap and DVWA explosion
2022 届的应届生都找到工作了吗?做自媒体可以吗?
在软件工程领域,搞科研的这十年!
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
UE4_ Ue5 panoramic camera
Unity3d learning notes 4 - create mesh advanced interface
Cloud native data warehouse analyticdb MySQL user manual
What are the similarities and differences between smart communities and smart cities
First week of July
MySQL Index Optimization Practice I
Spark 离线开发框架设计与实现
ArcGIS: two methods of attribute fusion of the same field of vector elements
Unity3D学习笔记6——GPU实例化(1)
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree