top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Check if a String Could Be a Palindrome


You have probably been tasked at some point or another to write a program that checks whether or not a given string is a palindrome. If not, try it! It's a good exercise.


The purpose of this blog is not to check for palindromes, but to check whether or not a string, if it were arranged in a certain way, could be a palindrome.


But what is a palindrome?

If you are not already familiar with the term, a palindrome is a string of characters, alphabetical or numerical, that are symmetric around their middle. A phrase like "Was it a car or a cat I saw?" is a palindrome, or even a date such as 21/9/21. Try reading them backwards and you'll find that they read the same (whitespace characters not considered).


An example of a string that could be a palindrome if rearranged is "cccaa". Its palindrome form could be "cacac" or "accca". A string like "fgfa" cannot be a palindrome no matter how many ways you try to arrange it.


So how do we check if a string could be a palindrome? It's simple. First, let's get user's input:

string = input('Please enter a string you wish to check: ')

After that let's begin our operation. Let's go over each character in the given string and count its number of occurences.

odd_count = 0
string_set = ''.join(set(string)) # get all unique characters in string
for char in string_set:
    if string.count(char) % 2:
        odd_count += 1

if odd_count > 1:
    print(f'{string} cannot be a palindrome')
else:
    print(f'{string} can be a palindrome')

Did you catch that? A string must contain at most one character with an odd frequency of occurence; meaning it appears in the string an odd number of times. The rest of the characters must have an even frequency of occurence. Brilliant!


2 comentários


Aina Adekunle
Aina Adekunle
23 de set. de 2021

Brilliant indeed! I like the way you presented it.

Curtir

Data Insight
Data Insight
23 de set. de 2021

Excellent!

Curtir

COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page