concatenation of the two wordsGiven a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome.For example,given the list ["code", "edoc", "da", "d"], return [(0, 1), (1, 0), (2, 3)].
Question
concatenation of the two wordsGiven a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome.For example,given the list ["code", "edoc", "da", "d"], return [(0, 1), (1, 0), (2, 3)].
Solution
Sure, here is a Python solution for the problem:
def palindrome_pairs(words):
def is_palindrome(check):
return check == check[::-1]
words = {word: i for i, word in enumerate(words)}
valid_pals = []
for word, k in words.items():
n = len(word)
for j in range(n+1):
pref = word[:j]
suf = word[j:]
if is_palindrome(pref):
back = suf[::-1]
if back != word and back in words:
valid_pals.append((words[back], k))
if j != n and is_palindrome(suf):
back = pref[::-1]
if back != word and back in words:
valid_pals.append((k, words[back]))
return valid_pals
print(palindrome_pairs(["code", "edoc", "da", "d"]))
This solution works by:
- Creating a dictionary where the key is a word from the list and the value is the index of that word.
- Iterating over each word in the dictionary, and for each word, it checks every possible prefix and suffix of the word.
- If the prefix is a palindrome, it reverses the suffix and checks if this reversed suffix exists in the dictionary (and is not the word itself). If it does, it adds the pair to the result list.
- If the suffix is a palindrome and is not the whole word (to avoid duplicating the result), it reverses the prefix and checks if this reversed prefix exists in the dictionary. If it does, it adds the pair to the result list.
- Finally, it returns the list of valid pairs.
Similar Questions
Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome.For example,given the list ["code", "edoc", "da", "d"], return [(0, 1), (1, 0), (2, 3)].Sample Test CasesTest Case 1:Expected Output:Enter·the·words·of·the·list·separated·by·space:·code edoc da dPairs·of·unique·indices·that·make·palindrome·are:·[(0,·1),·(1,·0),·(2,·3)]
"Please create a pseudocode function that will check if all the words in an array are palindromes. For example, an input of ['racecar', 'noon', 'civic'] should return True, but an input of ['racecar', 'shoe', 'moon'] should return False."
Given a string, discover all non-single letter palindrome substrings.
Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "bobseesanna" can be created by some ways:* bobseesanna = bob + sees + anna* bobseesanna = bob + s + ee + s + anna* bobseesanna = b + o + b + sees + a + n + n + a...We want to take the value of function CountPal(s) which is the number of different ways to use the palindromes to create the string s by the above method.InputThe string sOutputThe value of function CountPal(s), taking the modulo of 1 000 000 007 (109+7)Limitations0 < |s| <= 1000Sample 1:InputOutputbobseesanna18
You are given a singly linked list, determine if it is a palindrome.A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).Write a function that takes the head of a linked list as input and returns True if the linked list is a palindrome, and False otherwise.Sample input:4 //size1 2 2 1 //listOutput:TrueExplanation:The linked list reads the same forward and backward, making it a palindrome.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.