Given an array arr[], the task is to combine all the elements in the array sequentially and check if it is a palindrome.
Examples:
Input: arr[] ={1 , 69 , 54 , 45 , 96 , 1}
Output: palindrome
Explanation: The number formed by combining all the elements is “1695445961” which is a palindrome
Input: arr[] ={2 , 73 , 95 , 59 , 96 , 2}
Output: not palindrome
Explanation: The number formed by combining all the elements is “2739559962” which is not a palindrome
Method 1: Using map() and join()
- Convert each element of the list to a string using map() function.
- Join the list using join() function.
- Check if it is a palindrome.
- If yes then print palindrome.
- If no print, not a palindrome.
Below is the implementation of the above approach:
Python3
# function to check palindrome def checkPalindrome(string): # reverse the string rev = string[:: - 1 ] # checking if string is equal to reverse if (string = = rev): return True else : return False # function to convert list to single number string def joinArray(lis): # convert the elements of list to string lis = list ( map ( str , lis)) # converting list to string number = ''.join(lis) # checking if it is palindrome if (checkPalindrome(number)): return True else : return False # Driver code lis = [ 1 , 76 , 39 , 93 , 67 , 1 ] if (joinArray(lis)): print ( "Palindrome" ) else : print ( "not Palindrome" ) |
Palindrome
Time Complexity: O(n)
Method 2: Using type casting and string concatenation
- Take an empty string say str.
- Traverse through the list and convert each element to string using type casting
- Add this to str using string concatenation
- Check if str is a palindrome
Below is the implementation of the above approach:
Python3
# function to check palindrome def checkPalindrome(string): # reverse the string rev = string[:: - 1 ] # checking if string is equal to reverse if (string = = rev): return True else : return False # function to convert list to single number string def joinArray(lis): # defining empty string as number number = "" # convert the elements of list to string using type conversion for i in lis: # converting to string i = str (i) # concat this to string number = number + i # checking if it is palindrome if (checkPalindrome(number)): return True else : return False # Driver code lis = [ 1 , 76 , 39 , 93 , 67 , 1 ] if (joinArray(lis)): print ( "Palindrome" ) else : print ( "not Palindrome" ) |
Palindrome
Method 3 : Using str(),list(),extend(),join() and reverse() methods
Python3
lis = [ 1 , 76 , 39 , 93 , 67 , 1 ] s = "" for i in lis: s + = str (i) x = list (s) y = [] y.extend(x) x.reverse() if (x = = y): print ( "Palindrome" ) else : print ( "not Palindrome" ) |
Palindrome
Method 4 : Using reduce():
Algorithm:
- Import the “reduce” function from the “functools” module.
- Create a list of integers “lst”.
- Use the “reduce” function to concatenate all the integers in the list into a single string “s”.
- Check whether the string “s” is equal to its reverse.
- If “s” is equal to its reverse, print “Palindrome”; otherwise, print “Not Palindrome”.
Python3
from functools import reduce lst = [ 1 , 76 , 39 , 93 , 67 , 1 ] # concatenate all numbers into a single string s = reduce ( lambda x, y: str (x) + str (y), lst) # compare string with its reverse if s = = s[:: - 1 ]: print ( "Palindrome" ) else : print ( "Not Palindrome" ) #This code is contributed by Jyothi Pinjala. |
Palindrome
Time complexity: O(n), where n is the number of integers in the list “lst”. This is because the reduce function takes O(n) time to concatenate all the integers into a single string, and checking whether the string is equal to its reverse takes O(n/2) time.
Auxiliary Space: O(n), where n is the number of integers in the list “lst”. This is because the reduce function creates a new string of length n by concatenating all the integers in the list.