当前位置:网站首页>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();
}
}
边栏推荐
- Inftnews | the wide application of NFT technology and its existing problems
- 做自媒体视频剪辑怎么赚钱呢?
- 系统设计概述
- B_QuRT_User_Guide(37)
- When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
- Deep understanding of MySQL lock and transaction isolation level
- Tree background data storage (using webmethod) [easy to understand]
- 城联优品作为新力量初注入,相关上市公司股价应声上涨150%
- Bit operation
- MySQL Index Optimization Practice II
猜你喜欢
随机推荐
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
ROS2专题(03):ROS1和ROS2的区别【02】
Dynamics 365 find field filtering
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
海内外技术人们“看”音视频技术的未来
CAIP2021 初赛VP
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
在软件工程领域,搞科研的这十年!
Network security - Eternal Blue
[microservices SCG] gateway integration Sentinel
Grid
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
What are the similarities and differences between smart communities and smart cities
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
Mysql索引优化实战二
深入理解Mysql锁与事务隔离级别
CXF call reports an error. Could not find conduct initiator for address:
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
![Ros2 topic (03): the difference between ros1 and ros2 [01]](/img/20/39d47c93400050a7bc8ad7efea51b3.png)








