当前位置:网站首页>Dictionary tree leetcode seven hundred and forty-five
Dictionary tree leetcode seven hundred and forty-five
2022-07-18 22:42:00 【Lu 727】
class TrieTree {
// A prefix , A suffix
private Node root1;// The root node , No value
private Node root2;
// Internal node class
private class Node{
private Node childs[];// The following is marked as 26 Letters , Store the next node
private boolean isLeaf;// Is it a leaf node
private List<Integer> idxs;// Pass the word index of this node
public Node(){
childs=new Node[26];
isLeaf=false;
idxs=new ArrayList<>();
}
public boolean contains(char key){
return childs[key-'a']!=null;
}
public Node get(char key){
return childs[key-'a']==null?null:childs[key-'a'];
}
public void put(char key,Node n){
childs[key-'a']=n;
}
}
public TrieTree(){
root1=new Node();
root2=new Node();
}
// Add string
public void put(String word,int idx){
put(root1,root2,word,idx);
}
private void put(Node root1,Node root2,String word,int idx){
char[] chs=word.toCharArray();
int n=chs.length;
Node cur2=root2;
Node cur1=root1;
for (int i = 0; i < n; i++) {
// If one of these characters is not included , Create a new node
if(!cur1.contains(chs[i])){
cur1.put(chs[i],new Node());
}
if(!cur2.contains(chs[n-1-i])){
cur2.put(chs[n-1-i],new Node());
}
cur1=cur1.get(chs[i]);// If it contains, continue to look for the next character
cur2=cur2.get(chs[n-1-i]);
// For a word , If it passes through a node , Then record to this node
// When looking for a prefix , The last node records all word indexes with this prefix
cur1.idxs.add(idx);
cur2.idxs.add(idx);
}
// Reach the end of the word
cur1.isLeaf=true;
cur2.isLeaf=true;
}
List<Integer> search(Node root,String pre,int idx){
// To the end
if(idx==pre.length())
return root.idxs;
char c=pre.charAt(idx);
if(root.get(c)!=null)
return search(root.get(c),pre,idx+1);
else return null;
}
}
TrieTree t;
public WordFilter(String[] words) {
t=new TrieTree();
for(int i=0;i<words.length;i++){
t.put(words[i],i);
}
}
public int f(String a, String b) {
StringBuilder sb=new StringBuilder(b);
// Because the index increases from small to large when adding words , Then the node idxs Must be orderly
List<Integer> s1=t.search(t.root1,a,0);// have a Prefix
List<Integer> s2=t.search(t.root2,sb.reverse().toString(),0);// have b suffix
// Look for the largest index that exists in both lists from back to front
if(s1!=null&&s2!=null){
for (int i = s1.size()-1,j=s2.size()-1; i >=0 ; i--) {
while(j>=0){
if(s2.get(j).equals(s1.get(i)))//Integer Type comparison
return s1.get(i);
if(s2.get(j)<s1.get(i))
break;
j--;
}
}
}
return -1;
}边栏推荐
- Which securities company has a low handling fee for opening an account, and which stock is safe to open an account
- Definition and usage of several special standards of C language
- Transfer function
- 416.分割等和子集·背包问题·动态规划
- Activity review | in depth chat with mindspire: how open source explores the whole scene of AI field
- Leetcode45. Jumping game II
- In addition to Chang'an, these four domestic brands also use "Lexus face". Has Chinese design regressed?
- After reading this article, I will teach you to play with vulnhub, the penetration test target machine -- evilbox one
- 解题-->在线OJ(十六)
- Kaggle file download (weights, inference files...)
猜你喜欢

Uniapp Basics

7月7日易用性SIG技术分享活动精彩回顾

Source code analysis of ArrayList

Modify server password
![[UCOS III source code analysis] - event flag group](/img/5d/7fd1cff6eb8a1f08cefa6d37ed0b0a.png)
[UCOS III source code analysis] - event flag group

Graduation season -- common interview questions in database

DeepMind最新114页报告《多智能体强化学习中的新兴易货贸易行为》

Three sides of headlines + four sides of Alibaba + five sides of Tencent took the offer to share the summary, and finally joined Alibaba

Common problems of database

Tencent is all around. It's silly to ask
随机推荐
Detailed explanation of bean's life cycle
封装、获取系统用户信息、角色及权限控制
The 100 billion yuan universe market is the new driving force of soul and Yingke
[动态规划]DP23 不相邻取数-简单
Graduation season -- common interview questions in database
Honghu Wanlian Zhiyuan development board is officially integrated into the openharmony backbone
LINGO求解分段函数最大(小)值
JS binary search pseudocode
LCD display tear solution
[UCOS III source code analysis] - event flag group
看完这篇 教你玩转渗透测试靶机vulnhub——EvilBox-One
修改服务器密码
员工管理系统编写过程中摘要
Common problems of database
A New Optimizer Using Particle Swarm Theory
Marvell88q5192 switch debugging record (bsta1000b platform)
第3章业务功能开发(查看市场活动明细)
Visual studio production environment configuration scheme: slowcheetah
大疆校招测评题--循环赛问题
除了长安,这四个国产品牌也用“雷克萨斯脸”,中国设计倒退了?