-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circle of strings.java
38 lines (30 loc) · 1018 Bytes
/
Circle of strings.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
class Solution {
void dfs(int i, List<List<Integer>> graph, boolean[] visit) {
visit[i] = true;
for(int idx: graph.get(i))
if(visit[idx] != true)
dfs(idx,graph,visit);
}
public int isCircle(String arr[]) {
int in[] = new int[26];
int out[] = new int[26];
List<List<Integer>> adj = new ArrayList<>();
for(int i=0; i<26; i++) adj.add(new ArrayList<>());
for(String s: arr) {
int n = s.length();
in[s.charAt(0) - 'a']++;
out[s.charAt(n-1)-'a']++;
adj.get(s.charAt(0)-'a').add(s.charAt(n-1)-'a');
}
for(int i=0; i<26; i++) {
if(in[i] != out[i]) return 0;
}
boolean visit[] = new boolean[26];
dfs(arr[0].charAt(0)-'a',adj,visit);
for(int i=0; i<26; i++) {
if(in[i] > 0 && visit[i] != true)
return 0;
}
return 1;
}
}