-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircleOfStrings.java
More file actions
69 lines (58 loc) · 2.22 KB
/
circleOfStrings.java
File metadata and controls
69 lines (58 loc) · 2.22 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
class Solution {
public int isCircle(String arr[]) {
int n = arr.length;
if (n == 0) return 0;
// Create graph to store the in-degree and out-degree of each character.
int[] inDegree = new int[26];
int[] outDegree = new int[26];
// Adjacency list to store the edges between characters.
ArrayList<Integer>[] adj = new ArrayList[26];
for (int i = 0; i < 26; i++) {
adj[i] = new ArrayList<>();
}
// Fill in the degrees and adjacency list.
for (String str : arr) {
int first = str.charAt(0) - 'a';
int last = str.charAt(str.length() - 1) - 'a';
// Increment the out-degree of the first character and in-degree of the last character
outDegree[first]++;
inDegree[last]++;
// Add edge from first character to last character
adj[first].add(last);
}
// 1. Check if in-degree and out-degree of every character are the same
for (int i = 0; i < 26; i++) {
if (inDegree[i] != outDegree[i]) {
return 0;
}
}
// 2. Check if the graph is strongly connected
// Find the first character that has a degree > 0
int start = -1;
for (int i = 0; i < 26; i++) {
if (outDegree[i] > 0) {
start = i;
break;
}
}
// Perform DFS to check if all nodes with a degree > 0 are reachable
boolean[] visited = new boolean[26];
dfs(start, adj, visited);
// Check if all characters with a non-zero degree are visited
for (int i = 0; i < 26; i++) {
if (outDegree[i] > 0 && !visited[i]) {
return 0;
}
}
return 1; // Circle can be formed
}
// DFS helper function to explore the graph
private void dfs(int node, ArrayList<Integer>[] adj, boolean[] visited) {
visited[node] = true;
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
dfs(neighbor, adj, visited);
}
}
}
}