Geeksforgeeks

Error

×

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:**

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

**Example 2:**

**Input:**
N = 4
A[] = { "ab" , "bc", "cd", "da" }
**Output:**
1
**Explaination:**
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)

**Constraints:**

1 ≤ N ≤ 10^{4}

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.

Click here to view old Disqus comments.

Login 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?

YesLoading...

Circle of strings

...