Movatterモバイル変換


[0]ホーム

URL:


Open In App

Given a string, the task is to check whether it is a palindrome. A palindrome is astringthat reads the same forward and backward.For example, "madam" is a palindrome, while "hello" is not.

Using Two Pointer Technique

This method compares characters from both ends moving toward the center. It is the most efficient because it checks only half of the string without creating extra copies.

Python
s="malayalam"i,j=0,len(s)-1is_palindrome=Truewhilei<j:ifs[i]!=s[j]:is_palindrome=Falsebreaki+=1j-=1ifis_palindrome:print("Yes")else:print("No")

Output
Yes

Explanation:

  • while loopcompares characters from both ends towards the center as long as i < j.
  • if no mismatch is found, the pointers move inward for the next comparison
  • After the loop, it prints "Yes" if is_palindromeis True, otherwise "No".

Using all() with Generator Expression

This method checks if all mirrored characters are equal using a generator expression. The string is a palindrome if all comparisons pass.

Python
s="malayalam"ifall(s[i]==s[-i-1]foriinrange(len(s)//2)):print("Yes")else:print("No")

Output
Yes

Explanation:

  • range(len(s)//2) generates indices for the first half of the string.
  • s[-i-1] accesses characters from the end of the string.
  • s[i] == s[-i-1] checks equality for mirrored positions.
  • all() returns True only if every comparison is True.

Using Slicing

This method reverses the string usingslicing(s[::-1]) and compares it with the original string. If both match, the string is a palindrome.

Python
s="malayalam"ifs==s[::-1]:print("Yes")else:print("No")

Output
Yes

Explanation: It compares the string s with its reverses[::-1]. If they are equal, it prints "Yes" otherwise, it prints "No".

Using reversed() + join()

This method creates a reversed version of the string usingreversed() andjoin(), then compares it with the original string. If they match, it is a palindrome.

Python
s="geeks"rev=''.join(reversed(s))ifs==rev:print("Yes")else:print("No")

Output
No

Explanation: It creates a reversed version of the string with''.join(reversed(s)) and compares it with the original string. If they are equal, it prints "Yes" otherwise, it prints "No".


Check For Palindrome In Python
Visit Courseexplore course icon
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp