Open In App

Find if an array of strings can be chained to form a circle | Set 2

Last Updated : 11 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Share
13 Likes
Like
Report

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.

Examples: 

Input: arr[] = {"geek", "king"}
Output: Yes, the given strings can be chained.
Note that the last character of first string is same
as first character of second string and vice versa is
also true.

Input: arr[] = {"for", "geek", "rig", "kaf"}
Output: Yes, the given strings can be chained.
The strings can be chained as "for", "rig", "geek" 
and "kaf"

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", "baa", "aab"};
Output: Yes, the given strings can be chained.
The strings can be chained as "aaa", "aab", "bbb" 
and "baa"

Input: arr[] = {"aaa"};
Output: Yes

Input: arr[] = {"aaa", "bbb"};
Output: No

Input  : arr[] = ["abc", "efg", "cde", "ghi", "ija"]
Output : Yes
These strings can be reordered as, “abc”, “cde”, “efg”,
“ghi”, “ija”

Input : arr[] = [“ijk”, “kji”, “abc”, “cba”]
Output : No

We strongly recommend that you practice it, before moving on to the solution.

We have discussed one approach to this problem in the below post. 
Find if an array of strings can be chained to form a circle | Set 1

In this post, another approach is discussed. We solve this problem by treating this as a graph problem, where vertices will be the first and last character of strings, and we will draw an edge between two vertices if they are the first and last character of the same string, so a number of edges in the graph will be same as the number of strings in the array. 
Graph representation of some string arrays are given in the below diagram, 

array of strings that can be chained to form a circle

Now it can be clearly seen after graph representation that if a loop among graph vertices is possible then we can reorder the strings otherwise not. As in the above diagram’s example, a loop can be found in the first and third array of string but not in the second array of string. Now to check whether this graph can have a loop which goes through all the vertices, we’ll check two conditions, 

  1. Indegree and Outdegree of each vertex should be the same.
  2. The graph should be strongly connected.

The first condition can be checked easily by keeping two arrays, in and out for each character. For checking whether a graph is having a loop which goes through all vertices is the same as checking complete directed graph is strongly connected or not because if it has a loop which goes through all vertices then we can reach to any vertex from any other vertex that is, the graph will be strongly connected and the same argument can be given for reverse statement also. 

Now for checking the second condition we will just run a DFS from any character and visit all reachable vertices from this, now if the graph has a loop then after this one DFS all vertices should be visited, if all vertices are visited then we will return true otherwise false so visiting all vertices in a single DFS flags a possible ordering among strings

C++
Java Python3 C# JavaScript

Output
Ordering is possible

Time complexity: O(n)
Auxiliary Space: O(n)

 


Article Tags :
Practice Tags :

Similar Reads