-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlien Dictionary.java
More file actions
153 lines (125 loc) · 3.67 KB
/
Alien Dictionary.java
File metadata and controls
153 lines (125 loc) · 3.67 KB
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
146
147
148
149
150
151
152
153
//{ Driver Code Starts
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
import java.math.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.next());
while(t-- > 0)
{
int n = Integer.parseInt(sc.next());
int k = Integer.parseInt(sc.next());
String[] words = new String[n];
for(int i=0;i<n;i++)
{
words[i] = sc.next();
}
Solution ob = new Solution();
// System.out.println(T.findOrder(words,k));
String order = ob.findOrder(words,n,k);
if(order.length() == 0){
System.out.println(0);
continue;
}
String temp[] = new String[n];
for(int i=0;i<n;i++)
temp[i] = words[i];
Arrays.sort(temp, new Comparator<String>(){
@Override
public int compare(String a, String b) {
int index1 = 0;
int index2 = 0;
for(int i = 0; i < Math.min(a.length(), b.length())
&& index1 == index2; i++) {
index1 = order.indexOf(a.charAt(i));
index2 = order.indexOf(b.charAt(i));
}
if(index1 == index2 && a.length() != b.length())
{
if(a.length() < b.length())
return -1;
else
return 1;
}
if(index1 < index2)
return -1;
else
return 1;
}
});
int flag = 1;
for(int i=0;i<n;i++)
{
if(!words[i].equals(temp[i]))
{
flag = 0;
break;
}
}
System.out.println(flag);
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution
{
public String findOrder(String [] dict, int N, int K)
{
// Write your code here
ArrayList<ArrayList<Integer>> adj=new ArrayList<>();
for(int i=0;i<K;i++)
adj.add(new ArrayList<>());
for(int i=0;i<N-1;i++)
{
String a=dict[i];
String b=dict[i+1];
for(int j=0;j<Math.min(a.length(),b.length());j++)
{
if(a.charAt(j)!=b.charAt(j))
{
adj.get(a.charAt(j)-'a').add(b.charAt(j)-'a');
break;
}
}
}
ArrayList<Integer> ans= topo(adj,K);
String res="";
for(int i: ans)
{
res=res+ ((char)(i+(int)('a')));
}
return res;
}
private ArrayList<Integer> topo (ArrayList<ArrayList<Integer>> adj, int K)
{
int in[]=new int[K];
for(int i=0;i<K;i++)
{
for(int a:adj.get(i))
in[a]++;
}
Queue<Integer> uwu=new LinkedList<>();
for(int i=0;i<K;i++)
{
if(in[i]==0)
uwu.add(i);
}
ArrayList<Integer> ans=new ArrayList<>();
while(!uwu.isEmpty())
{
int node=uwu.peek();
uwu.remove();
ans.add(node);
for(int a:adj.get(node))
{
in[a]--;
if(in[a]==0)
uwu.add(a);
}
}
return ans;
}
}