- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if the characters in a string form a Palindrome in O(1) extra space in Python
Suppose we have a string s. This string can contain lowercase characters, other special characters and numbers. We have to check whether only the letters present in the string are palindromic or not. Here one constraint is that there we are not allowed to use extra space to solve this problem.
So, if the input is like s = "ra$5ce58car", then the output will be True, as the letters are forming "racecar" which is palindrome.
To solve this, we will follow these steps −
- Define a function first_letter_index() . This will take str, left, right
- index := -1
- for i in range left to right + 1, do
- if str[i] is lower case alphabet, then
- index := i
- come out from loop
- if str[i] is lower case alphabet, then
- return index
- Define a function last_letter_index() . This will take str, left, right
- index := -1
- for i in range left to right - 1, decrease by 1, do
- if str[i] is lower case alphabet, then
- index := i
- come out from loop
- return index
- From the main method do the following:
- left := 0, right := size of str - 1
- flag := True
- for i in range 0 to size of str, do
- left := first_letter_index(str, left, right)
- right := last_letter_index(str, right, left)
- if right < 0 or left < 0, then
- come out from loop
- if str[left] is same as str[right], then
- left := left + 1
- right := right - 1
- go for next iteration
- flag := False
- come out from loop
- if str[i] is lower case alphabet, then
- return flag
Let us see the following implementation to get better understanding −
Example Code
def first_letter_index(str, left, right): index = -1 for i in range(left, right + 1): if str[i] >= 'a' and str[i] <= 'z' : index = i break return index def last_letter_index(str, left, right): index = -1 for i in range(left, right - 1, -1) : if str[i] >= 'a' and str[i] <= 'z': index = i break return index def solve(str): left = 0 right = len(str) - 1 flag = True for i in range(len(str)) : left = first_letter_index(str, left, right) right = last_letter_index(str, right, left) if right < 0 or left < 0: break if str[left] == str[right]: left += 1 right -= 1 continue flag = False break return flag s = "ra$5ce58car" print(solve(s))
Input
"ra$5ce58car"
Output
True
- Related Articles
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Find maximum in a stack in O(1) time and O(1) extra space in C++
- Check if a string can be rearranged to form special palindrome in Python
- Find the maximum repeating number in O(n) time and O(1) extra space in Python
- Find duplicates in O(n) time and O(1) extra space - Set 1 in C++
- Python - Check If All the Characters in a String Are Alphanumeric?
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Rearrange characters to form palindrome if possible in C++
- Rearrange array in alternating positive & negative items with O(1) extra space in C++
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Check if characters of one string can be swapped to form other in Python
- Check if a string has white space in JavaScript?
- Check if any anagram of a string is palindrome or not in Python
- Program to check string is palindrome with lowercase characters or not in Python

Advertisements