Circle of strings
Medium Accuracy: 39.13% Submissions: 9192 Points: 4

Given an array of lowercase strings A[] of size N, determine if the strings can be chained together to form a circle.
A string X can be chained together with another string Y if the last character of X is same as first
character of Y. If every string of the array can be chained, it will form a circle.

For example, for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"

Example 1:

N = 3
A[] = { "abc", "bcd", "cdf" }
These strings can't form a circle 
because no string has 'd'at the starting index.

Example 2:

N = 4
A[] = { "ab" , "bc", "cd", "da" }
These strings can form a circle 
of strings.

Your Task:
You don't need to read input or print output. Your task is to complete the function isCircle() which takes the length of the array N and the array A as input parameters and returns 1 if we can form a circle or 0 if we cannot.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

1 ≤ N ≤ 104
1 ≤ Length of strings ≤ 20

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Circle of strings

Output Window