当前位置:网站首页>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);
}
}
边栏推荐
- VSCode 配置使用 PyLint 语法检查器
- Similarities and differences between switches and routers
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- Mysql怎样控制replace替换的次数?
- 一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
- NDK beginner's study (1)
- 属性关键字Aliases,Calculated,Cardinality,ClientName
- [AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
- Common response status codes
- SAKT方法部分介绍
猜你喜欢
libSGM的horizontal_path_aggregation程序解读
Vmware 与主机之间传输文件
用例图
Vscode configuration uses pylint syntax checker
Data flow diagram, data dictionary
Selenium Library
[Reading stereo matching papers] [III] ints
GVIM [III] [u vimrc configuration]
Redis 核心数据结构 & Redis 6 新特性详
Transferring files between VMware and host
随机推荐
ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
The longest ascending subsequence model acwing 1012 Sister cities
【网络安全】sql注入语法汇总
搜索引擎接口
搜索框效果的实现【每日一题】
Cesium 已知一点经纬度和距离求另一个点的经纬度
请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
Oracle Linux 9.0 正式发布
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
Excuse me, does PTS have a good plan for database pressure measurement?
Lavarel之环境配置 .env
通过 iValueConverter 给datagrid 的背景颜色 动态赋值
Laravel form builder uses
UML 状态图
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
SAKT方法部分介绍
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
Interface automation test - solution of data dependency between interfaces
Mrs offline data analysis: process OBS data through Flink job