Python Interview Questions Series-1
Checking the probability of a given string whether it can form a palindrome or not in Python

Python Interview Questions Series-1

In this article, we are going to discuss the palindrome probability for a given string.

A palindrome is a word, number, phrase, or another sequence of characters that reads the same backward as forward, such as madam or racecar.

Let's get into the scenario question coding:-


def check_probability_of_palindrome(s)
	    dictonary1=dict()
	

	    for i in list(s):
	        if i in dictonary1:
	            dictonary1[i]+=1
	        else:
	            dictonary1[i]=1
	    
	    k=[]
	    for i,j in dictonary1.items():
	        k.append(j)
	        
	    k1=[]
	    for i in k:
	        if i%2!=0:
	            k1.append(i)
	    if len(k1)==1:
	        print(True)
	    else:
	        print(False)
	

	# check        
	check_probability_of_palindrome("abb")
	>>> True
	

	## other examples
	# Input : "mdaam"  
	# output: true
	# Input : "abb"
	# Output : true
	# Input : "civic"
	# output : true
	# input : "rock"
	# output : false:        

That's all for now...

This Code might look naive but serves the purpose.

Note:- If anyone has a better approach to generalizing this code happy to embed it in my script.

That’s all for now…Happy Learning….

Please do like and follow back my profile…Don't forget to Comment…




Seyram Kwame

Engineering Graduate Assistant at University of Arkansas

1 年

Hey!, I need someone to help me. I really want to be a genius in python. I have have the interest but I think i need help ??

回复
Sairam P L

Cloud Data Engineer || Blogger || Medium writings

2 年
回复

要查看或添加评论,请登录

Sairam P L的更多文章

其他会员也浏览了