Given an array of strings, find if the given strings can be chained to form a circle. A string X can be put before another string Y in a circle if the last character of X is the same as the first character of Y.
Eg.
Input: arr[] = {"aab", "bac", "aaa", "cda"}
Output: Yes, the given strings can be chained.
The strings can be chained as "aaa", "aab", "bac"
and "cda"Input: arr[] = {"aaa", "bbb"};
Output: NoN = 4
A[] = { "ab" , "bc", "cd", "de" }
Output:
false 


for(int i = 0; i < 26; i++){
if(inDegree[i] != outDegree[i]){
return false;
}
} void dfs(int start, vector < int > adj[], vector < bool > & visited) {
visited[start] = true;
for (auto it: adj[start]) {
if (visited[it] == false)
dfs(it, adj, visited);
}
}
bool IsConnected(int s, int V, vector < int > adj[], vector < bool > & mark) {
vector < bool > visited(26, false);
dfs(s, adj, visited);
for (int i = 0; i < 26; i++)
if (visited[i] == false && mark[i] == true)
return false;
return true;
}
int isCircle(int V, vector < string > A) {
vector < bool > mark(26, false);
vector < int > adj[26];
vector < int > indegree(26, 0), outdegree(26, 0);
for (int i = 0; i < V; i++) {
string s = A[i];
int u = s[0] - 'a';
int v = s[s.size() - 1] - 'a';
mark[u] = mark[v] = true;
indegree[v]++, outdegree[u]++;
adj[u].push_back(v);
}
for (int i = 0; i < 26; i++) {
if (indegree[i] != outdegree[i]) return false;
}
return IsConnected(A[0].front() - 'a', 26, adj, mark);
}for (int i = 0; i < V; i++)
if (adj[i].size() != indegree[i])
return false;