Given a string str. The string may contain lower-case letters, special characters, digits or even white-spaces. The task is to check, whether only the letters present in the string are forming a Palindromic combination or not without using any extra space.
Note: It is not allowed to use extra space to solve this problem. Also, the letters present in the string are in lower-case and the string may contain special characters, digits or even white-spaces along with lowercase letters.
Examples:
Input : str = “m a 343 la y a l am”
Output : YES
The characters in the string form the sequence “malayalam”, which is a palindrome.Input : str = “malayalam”
Output : YES
Approach:
- Create two utility functions to get the first and last position of characters present in the string.
- Start traversing the string, and keep finding the position of first and last characters everytime.
- If the first and last characters are same for every iteration and the string is traversed completely then print YES otherwise print NO.
Below is the implementation of above approach:
C++
// CPP program to check if the characters in // the given string forms a Palindrome in // O(1) extra space #include <iostream> using namespace std; // Utilty function to get the position of // first character in the string int firstPos(string str, int start, int end) { int firstChar = -1; // Get the position of first character // in the string for (int i = start; i <= end; i++) { if (str[i] >= 'a' && str[i] <= 'z') { firstChar = i; break; } } return firstChar; } // Utilty function to get the position of // last character in the string int lastPos(string str, int start, int end) { int lastChar = -1; // Get the position of last character // in the string for (int i = start; i >= end; i--) { if (str[i] >= 'a' && str[i] <= 'z') { lastChar = i; break; } } return lastChar; } // Function to check if the characters in // the given string forms a Palindrome in // O(1) extra space bool isPalindrome(string str) { int firstChar = 0, lastChar = str.length() - 1; bool ch = true; for (int i = 0; i < str.length(); i++) { firstChar = firstPos(str, firstChar, lastChar); lastChar = lastPos(str, lastChar, firstChar); // break, when all letters are checked if (lastChar < 0 || firstChar < 0) break; if (str[firstChar] == str[lastChar]) { firstChar++; lastChar--; continue; } // if mismatch found, break the loop ch = false; break; } return (ch); } // Driver code int main() { string str = "m a 343 la y a l am"; if (isPalindrome(str)) cout << "YES"; else cout << "NO"; return 0; } |
Java
// Java program to check if // the characters in the given // string forms a Palindrome // in O(1) extra space import java.io.*; class GFG { // Utilty function to get // the position of first // character in the string static int firstPos(String str, int start, int end) { int firstChar = -1; // Get the position of // first character in // the string for(int i = start; i <= end; i++) { if(str.charAt(i) >= 'a'&& str.charAt(i) <= 'z') { firstChar = i; break; } } return firstChar; } // Utilty function to get // the position of last // character in the string static int lastPos(String str, int start, int end) { int lastChar = -1; // Get the position of last // character in the string for(int i = start; i >= end; i--) { if(str.charAt(i) >= 'a'&& str.charAt(i) <= 'z') { lastChar = i; break; } } return lastChar; } // Function to check if the // characters in the given // string forms a Palindrome // in O(1) extra space static boolean isPalindrome(String str) { int firstChar = 0, lastChar = str.length() - 1; boolean ch = true; for (int i = 0; i < str.length(); i++) { firstChar = firstPos(str, firstChar, lastChar); lastChar = lastPos(str, lastChar, firstChar); // break, when all // letters are checked if (lastChar < 0 || firstChar < 0) break; if (str.charAt(firstChar) == str.charAt(lastChar)) { firstChar++; lastChar--; continue; } // if mismatch found, // break the loop ch = false; break; } return ch; } // Driver code public static void main (String[] args) { String str = "m a 343 la y a l am"; if(isPalindrome(str)) System.out.print("YES"); else System.out.println("NO"); } } // This code is contributed // by inder_verma. |
Python 3
# Python 3 program to check if the characters # in the given string forms a Palindrome in # O(1) extra space # Utilty function to get the position of # first character in the string def firstPos(str, start, end): firstChar = -1 # Get the position of first character # in the string for i in range(start, end + 1): if (str[i] >= 'a' and str[i] <= 'z') : firstChar = i break return firstChar # Utilty function to get the position of # last character in the string def lastPos(str, start, end): lastChar = -1 # Get the position of last character # in the string for i in range(start, end - 1, -1) : if (str[i] >= 'a' and str[i] <= 'z') : lastChar = i break return lastChar # Function to check if the characters in # the given string forms a Palindrome in # O(1) extra space def isPalindrome(str): firstChar = 0 lastChar = len(str) - 1 ch = True for i in range(len(str)) : firstChar = firstPos(str, firstChar, lastChar); lastChar = lastPos(str, lastChar, firstChar); # break, when all letters are checked if (lastChar < 0 or firstChar < 0): break if (str[firstChar] == str[lastChar]): firstChar += 1 lastChar -= 1 continue # if mismatch found, break the loop ch = False break return (ch) # Driver code if __name__ == "__main__": str = "m a 343 la y a l am" if (isPalindrome(str)): print("YES") else: print("NO") # This code is contributed by ita_c |
C#
// C# program to check if // the characters in the given // string forms a Palindrome // in O(1) extra space using System; class GFG { // Utilty function to get // the position of first // character in the string static int firstPos(string str, int start, int end) { int firstChar = -1; // Get the position of // first character in // the string for(int i = start; i <= end; i++) { if(str[i] >= 'a'&& str[i] <= 'z') { firstChar = i; break; } } return firstChar; } // Utilty function to get // the position of last // character in the string static int lastPos(string str, int start, int end) { int lastChar = -1; // Get the position of last // character in the string for(int i = start; i >= end; i--) { if(str[i] >= 'a'&& str[i] <= 'z') { lastChar = i; break; } } return lastChar; } // Function to check if the // characters in the given // string forms a Palindrome // in O(1) extra space static bool isPalindrome(string str) { int firstChar = 0, lastChar = str.Length - 1; bool ch = true; for (int i = 0; i < str.Length; i++) { firstChar = firstPos(str, firstChar, lastChar); lastChar = lastPos(str, lastChar, firstChar); // break, when all // letters are checked if (lastChar < 0 || firstChar < 0) break; if (str[firstChar] == str[lastChar]) { firstChar++; lastChar--; continue; } // if mismatch found, // break the loop ch = false; break; } return ch; } // Driver code public static void Main () { string str = "m a 343 la y a l am"; if(isPalindrome(str)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed // by inder_verma. |
PHP
<?php // PHP program to check if the // characters in the given // string forms a Palindrome // in O(1) extra space // Utilty function to get the // position of first character // in the string function firstPos($str, $start, $end) { $firstChar = -1; // Get the position of first // character in the string for ($i = $start; $i <= $end; $i++) { if ($str[$i] >= 'a' and $str[$i] <= 'z') { $firstChar = $i; break; } } return $firstChar; } // Utilty function to get the // position of last character // in the string function lastPos($str, $start, $end) { $lastChar = -1; // Get the position of last // character in the string for ( $i = $start; $i >= $end; $i--) { if ($str[$i] >= 'a' and $str[$i] <= 'z') { $lastChar = $i; break; } } return $lastChar; } // Function to check if the // characters in the given // string forms a Palindrome // in O(1) extra space function isPalindrome($str) { $firstChar = 0; $lastChar = count($str) - 1; $ch = true; for ($i = 0; $i < count($str); $i++) { $firstChar = firstPos($str, $firstChar, $lastChar); $lastChar = lastPos($str, $lastChar, $firstChar); // break, when all letters are checked if ($lastChar < 0 or $firstChar < 0) break; if ($str[$firstChar] == $str[$lastChar]) { $firstChar++; $lastChar--; continue; } // if mismatch found, // break the loop $ch = false; break; } return ($ch); } // Driver code $str = "m a 343 la y a l am"; if (isPalindrome($str)) echo "YES"; else echo "NO"; // This code is contributed // by inder_verma. ?> |
YES
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- To check a number is palindrome or not without using any extra space
- Length of longest palindrome list in a linked list using O(1) extra space
- Check if characters of a given string can be rearranged to form a palindrome
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Replace all occurrences of string AB with C without using extra space
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Remove duplicates from a string in O(1) extra space
- Rearrange characters to form palindrome if possible
- Longest substring whose characters can be rearranged to form a Palindrome
- Check if a string can be rearranged to form special palindrome
- Check if string remains palindrome after removing given number of characters
- Reverse individual words with O(1) extra space
- Check if characters of one string can be swapped to form other
- Check if characters of a given string can be used to form any N equal strings
- Minimal moves to form a string by adding characters or appending string itself
- Check if a doubly linked list of characters is palindrome or not
- Longest palindrome subsequence with O(n) space
- Minimum characters to be added at front to make string palindrome
- Find if string is K-Palindrome or not using all characters exactly once
- Find the player who rearranges the characters to get a palindrome string first
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

