
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count number of word concatenations are there in the list in python
Suppose we have a list of strings; we have to find the number of words that are concatenations of other words also in the list. We can reuse words when concatenating and concatenate any number of times.
So, if the input is like words = ["hello", "world", "helloworld", "famous", "worldfamous", "programming"], then the output will be 2, as "helloworld" is concatenation of "hello" and "world". "worldfamous" is concatenation of "world" and "famous".
To solve this, we will follow these steps:
- trie := a new map
- for each word in words, do
- layer := trie
- for each w in word, do
- if w is not in layer, then
- layer[w] := a new map
- layer := layer[w]
- if w is not in layer, then
- layer["*"] := an empty tuple
- Define a function dfs() . This will take word, num_concatenated_words
- layer := trie
- for each index i and word w in word, do
- if "*" is in layer, then
- if dfs(word[from index i to end], num_concatenated_words + 1) is True, then
- return True
- if w is not in layer, then
- return False
- if dfs(word[from index i to end], num_concatenated_words + 1) is True, then
- layer := layer[w]
- if "*" is in layer, then
- if "*" is in layer and num_concatenated_words >= 1, then
- return True
- return False
- From the main method, do the following:
- count := 0
- for each word in words, do
- count := count + dfs(word, 0)
- return count
Let us see the following implementation to get better understanding:
Example
class Solution: def solve(self, words): trie = {} for word in words: layer = trie for w in word: if w not in layer: layer[w] = {} layer = layer[w] layer["*"] = () def dfs(word, num_concatenated_words): layer = trie for i, w in enumerate(word): if "*" in layer: if dfs(word[i:], num_concatenated_words + 1): return True if w not in layer: return False layer = layer[w] if "*" in layer and num_concatenated_words >= 1: return True return False count = 0 for word in words: count += dfs(word, 0) return count ob = Solution() words = ["hello", "world", "helloworld", "famous", "worldfamous", "programming"] print(ob.solve(words))
Input
["hello", "world", "helloworld", "famous", "worldfamous", "programming"]
Output
2
- Related Articles
- Program to find number of subsequence that are present inside word list in python
- Python program to count occurrences of a word in a string
- Program to count number of elements in a list that contains odd number of digits in Python
- Program to count number of swaps required to change one list to another in Python?
- Write a python program to count occurrences of a word in string?
- Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion
- Program to count the number of consistent strings in Python
- Python program to count the number of spaces in string
- Golang Program to count the number of nodes in a linked list.
- Program to count number of configurations are there to fill area with dominos and trominos in C++
- Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion
- Program to count number of palindromic substrings in Python
- Program to count number of unhappy friends in Python
- Program to count number of homogenous substrings in Python
- Program to count number of nice subarrays in Python

Advertisements