-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp128.java
80 lines (75 loc) · 2.08 KB
/
p128.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*Given an array of words, find all shortest unique prefixes to represent each word in the given array. Assume that no word is prefix of another.
Example 1:
Input:
N = 4
arr[] = {"zebra", "dog", "duck", "dove"}
Output: z dog du dov
Explanation:
z => zebra
dog => dog
duck => du
dove => dov
Example 2:
Input:
N = 3
arr[] = {"geeksgeeks", "geeksquiz",
"geeksforgeeks"};
Output: geeksg geeksq geeksf
Explanation:
geeksgeeks => geeksg
geeksquiz => geeksq
geeksforgeeks => geeksf
Your task:
You don't have to read input or print anything. Your task is to complete the function findPrefixes() which takes the array of strings and it's size N as input and returns a list of shortest unique prefix for each word
Expected Time Complexity: O(N*length of each word)
Expected Auxiliary Space: O(N*length of each word)
Constraints:
1 ≤ N, Length of each word ≤ 1000*/
class TrieNode{
TrieNode children[];
int freq;
TrieNode(){
children=new TrieNode[26];
for(int i=0;i<26;i++){
children[i]=null;
}
freq=0;
}
}
public class Solution {
static String[] findPrefixes(String[] arr, int N) {
TrieNode root=new TrieNode();
String res[]=new String[N];
for(int i=0;i<N;i++){
insert(arr[i],root);
}
for(int i=0;i<N;i++){
res[i]=find(arr[i],root);
}
return res;
}
static void insert(String word,TrieNode root){
TrieNode curr=root;
for(int i=0;i<word.length();i++){
int idx=word.charAt(i)-'a';
if(curr.children[idx]==null){
curr.children[idx]=new TrieNode();
}
curr=curr.children[idx];
curr.freq++;
}
}
static String find(String key,TrieNode root){
TrieNode curr=root;
String ans="";
for(int i=0;i<key.length();i++){
int idx=key.charAt(i)-'a';
ans+=key.charAt(i);
curr=curr.children[idx];
if(curr.freq<=1){
break;
}
}
return ans;
}
};