当前位置:网站首页>LeetCode 648. Word replacement
LeetCode 648. Word replacement
2022-07-07 14:19:00 【Sasakihaise_】


【 Hash + string manipulation 】 take dictionary Put the words in HashSet in , Record the length of words during the addition process , Convenience is right sentence Prefix the words in .
class Solution {
// Hash + String interception 12:41
public String replaceWords(List<String> dictionary, String sentence) {
Set<String> set = new HashSet();
Set<Integer> length = new HashSet();
for (var st: dictionary) {
set.add(st);
length.add(st.length());
}
List<Integer> l = new ArrayList(length);
Collections.sort(l);
String[] strs = sentence.split(" ");
int n = l.size();
StringBuilder sb = new StringBuilder();
for (var str: strs) {
sb.append(" ");
var flag = 0;
for (var i = 0; i < n; i++) {
var m = l.get(i);
if (m >= str.length()) {
break;
} else {
var st = str.substring(0, m);
if (set.contains(st)) {
flag = 1;
sb.append(st);
break;
}
}
}
if (flag == 0) {
sb.append(str);
}
}
return sb.toString().strip();
}
}【 Dictionary tree 】 Yes dictionary Word construction in , Traverse sentence The words in , The prefix that matches first can be returned .
class Solution {
// Dictionary tree 1:15 33
class Trie {
Trie[] map = new Trie[26];
boolean is_end;
}
Trie root = new Trie();
public void add(String str) {
Trie node = root;
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map[c - 'a'] != null) {
node = node.map[c - 'a'];
} else {
Trie trie = new Trie();
node.map[c - 'a'] = trie;
node = trie;
}
}
node.is_end = true;
}
public String pre(StringBuilder sb, String str) {
Trie node = root;
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map[c - 'a'] == null) {
return str;
} else {
sb.append(c);
node = node.map[c - 'a'];
if (node.is_end) {
return sb.toString();
}
}
}
return str;
}
public String replaceWords(List<String> dictionary, String sentence) {
for (var str: dictionary) {
add(str);
}
String[] strs = sentence.split(" ");
StringBuilder sb = new StringBuilder();
for (var str: strs) {
sb.append(pre(new StringBuilder(), str));
sb.append(" ");
}
return sb.toString().strip();
}
}【 Dictionary tree + Hash table writing 】
class Solution {
// Dictionary tree 10:05 10:16
class Trie {
Map<Character, Trie> map = new HashMap();
boolean endOfWord = false;
}
Trie root = new Trie();
public void add(String str) {
Trie node = root;
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map.containsKey(c)) {
node = node.map.get(c);
} else {
Trie trie = new Trie();
node.map.put(c, trie);
node = trie;
}
}
node.endOfWord = true;
}
public String prefix(String str) {
Trie node = root;
StringBuilder sb = new StringBuilder();
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map.containsKey(c)) {
sb.append(c);
node = node.map.get(c);
if (node.endOfWord) {
return sb.toString();
}
} else {
return str;
}
}
return str;
}
public String replaceWords(List<String> dictionary, String sentence) {
for (var str: dictionary) {
add(str);
}
String strs[] = sentence.split(" ");
for (var i = 0; i < strs.length; i++) {
strs[i] = prefix(strs[i]);
}
return String.join(" ", strs);
}
}
边栏推荐
- 3D detection: fast visualization of 3D box and point cloud
- Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
- UML sequence diagram (sequence diagram)
- C # use TCP protocol to establish connection
- Attribute keywords aliases, calculated, cardinality, ClientName
- 【立体匹配论文阅读】【三】INTS
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- GVIM [III] [u vimrc configuration]
- Use case diagram
- IP address home location query full version
猜你喜欢

LeetCode每日一题(636. Exclusive Time of Functions)

使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )

Parsing of XML files

MRS离线数据分析:通过Flink作业处理OBS数据

最长上升子序列模型 AcWing 482. 合唱队形

The longest ascending subsequence model acwing 482 Chorus formation

Take you to master the three-tier architecture (recommended Collection)

带你掌握三层架构(建议收藏)
![Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]](/img/d3/20674983717d829489149b4d3bfedf.png)
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]

Redis 核心数据结构 & Redis 6 新特性详
随机推荐
Pert diagram (engineering network diagram)
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
Docker deploy Oracle
Hangdian oj2054 a = = B? ???
【立体匹配论文阅读】【三】INTS
LeetCode 648. 单词替换
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
Selenium Library
内存溢出和内存泄漏的区别
Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
bashrc与profile
JS get the current time, month, day, year, and the uniapp location applet opens the map to select the location
First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
Excuse me, why is it that there are no consumption messages in redis and they are all piled up in redis? Cerely is used.
Transferring files between VMware and host
AI人才培育新思路,这场直播有你关心的
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
3D Detection: 3D Box和点云 快速可视化
Is it safe to open an account online now? Which securities company should I choose to open an account online?