Program to Find Out if There is a Short Circuit in Input Words in Python

Suppose we have a list of words. We have to check the given words can be chained to form a circle. A word A can be placed in front of another word B in a chained circle if only the last character of A is identical to the first character of B. Every word has to be used and can be used only once (the first/last word will not be considered).

So, if the input is like words = ["ant","dog","tamarind","nausea","gun"], then the output will be True.

To solve this, we will follow these steps −

  • graph := a new key-value pair list

  • seen := a new set

  • inDegree := a new key-value pair list

  • outDegree := a new key-value pair list

  • for each word in words, do

    • start := word[0]

    • end := word[-1]

    • insert end at the end of graph[start]

    • outDegree[start] := outDegree[start] + 1

    • inDegree[end] := inDegree[end] + 1

  • for each node in outDegree, do

    • if outDegree[node] is not same as inDegree[node], then

      • return False

  • dfs(words[0,0])

  • return size of seen if it is same as size of graph

  • Define a function dfs() . This will take node.

    • add(node) in seen

    • for each child in graph[node], do

      • if child is not present in seen, then

      • dfs(child)


Let us see the following implementation to get better understanding −

 Live Demo

import collections
class Solution:
   def solve(self, words):
      self.graph = collections.defaultdict(list)
      self.seen = set()
      inDegree = collections.Counter()
      outDegree = collections.Counter()
      for word in words:
         start = word[0]
         end = word[-1]
         outDegree[start] += 1
         inDegree[end] += 1
      for node in outDegree:
         if outDegree[node] != inDegree[node]:
            return False
      return len(self.seen) == len(self.graph)
   def dfs(self, node):
      for child in self.graph[node]:
         if child not in self.seen:
ob = Solution()





Updated on: 23-Dec-2020


Kickstart Your Career

Get certified by completing the course

Get Started