-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 100: Find the longest string
146 lines (111 loc) · 3.61 KB
/
Day 100: Find the longest string
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Find the longest string
Given an array of strings arr[]. You have to find the longest string which is lexicographically smallest and also all of its prefix strings are already present in the array.
Example 1:
Input: ab a abc abd
Output: abc
Explanation: We can see that length of the longest
string is 3. And there are two string "abc" and "abd"
of length 3. But for string "abc" , all of its prefix
"a" "ab" "abc" are present in the array. So the
output is "abc".
Example 2:
Input: ab a aa abd abc abda abdd abde abdab
Output: abdab
Explanation: We can see that each string is fulfilling
the condition. For each string, all of its prefix
are present in the array.And "abdab" is the longest
string among them. So the ouput is "abdab".
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestString() which takes the array arr[] as input parameter and returns the longest string which is also lexicographically smallest.And if there is no such string return empty string.
Expected Time Complexity: O(NlogN)
Expected Space Complexity: O(N)
Constraints:
1 <= length of arr[] <= 103
1 <= arr[i].length <=30
*/
//{ Driver Code Starts
import java.io.*;
import java.util.*;
class StringArray
{
public static String[] input(BufferedReader br, int n) throws IOException
{
String[] s = br.readLine().trim().split(" ");
return s;
}
public static void print(String[] a)
{
for(String e : a)
System.out.print(e + " ");
System.out.println();
}
public static void print(ArrayList<String> a)
{
for(String e : a)
System.out.print(e + " ");
System.out.println();
}
}
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while(t-- > 0){
int n;
n = Integer.parseInt(br.readLine());
String[] arr = StringArray.input(br, n);
Solution obj = new Solution();
String res = obj.longestString(n, arr);
System.out.println(res);
}
}
}
// } Driver Code Ends
class Solution {
public static class TrieNode{
TrieNode[] node;
boolean isWord=false;
TrieNode() {
node = new TrieNode[26];
}
}
public static void addWord(TrieNode root,String s) {
int idx;
for(int i=0;i<s.length();i++) {
idx = s.charAt(i)-'a';
if(root.node[idx]==null) root.node[idx] = new TrieNode();
root = root.node[idx];
}
root.isWord=true;
}
public static boolean isValid(TrieNode root,String s) {
int idx;
for(int i=0;i<s.length();i++) {
idx = s.charAt(i)-'a';
// if(root.node[idx]==null) return false;
root = root.node[idx];
if(!root.isWord) return false;
}
return true;
}
public static String longestString(int n, String[] arr) {
// code here
TrieNode root = new TrieNode();
for(String s : arr) {
addWord(root,s);
}
String ans ="";
for(String s : arr) {
if(isValid(root,s)) {
if(ans.length()<s.length()) {
ans=s;
} else if(ans.length()==s.length()) {
if(ans.compareTo(s)>0) ans=s;
}
}
}
return ans;
}
}