- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of ways we can concatenate words to make palindromes in Python

Suppose we have a list of distinct words, we have to find the number of different ways we can concatenate two different words from the given list of words to make a palindrome.

So, if the input is like words = ["time", "emit", "mo", "m"], then the output will be 3, as we can make "timeemit", "emittime", and "mom".

To solve this, we will follow these steps −

res := 0

ln := number of words in the array

for k in range 0 to 1, do

for i in range 0 to ln − 1, do

for j in range i + 1 to ln − 1, do

res := res + (1 when words[i] concatenate words[j] is palindrome, otherwise 0)

words := words in reverse order

return res

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, words): def is_palindrome(w1, w2): w3 = w1 + w2 return w3 == w3[::−1] res = 0 ln = len(words) for k in range(2): for i in range(ln): for j in range(i + 1, ln): res += is_palindrome(words[i], words[j]) words = words[::−1] return res ob = Solution() words = ["time", "emit", "mo", "m"] print(ob.solve(words))

## Input

["time", "emit", "mo", "m"]

## Output

3

- Related Questions & Answers
- Program to find possible number of palindromes we can make by trimming string in Python
- Program to count number of unique palindromes we can make using string characters in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find maximum number of people we can make happy in Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to find number of ways we can reach to the next floor using stairs in Python
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to count number of ways we can throw n dices in Python
- Program to count number of ways we can distribute coins to workers in Python
- Program to count number of ways we can make a list of values by splitting numeric string in Python
- Program to find how many ways we can climb stairs in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to count number of words we can generate from matrix of letters in Python

Advertisements