Given 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 the first and last characters every time.
- If the first and last characters are the same for every iteration and the string is traversed completely, then print YES, otherwise print NO.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int firstPos(string str, int start, int end)
{
int firstChar = -1;
for ( int i = start; i <= end; i++) {
if (str[i] >= 'a' && str[i] <= 'z' ) {
firstChar = i;
break ;
}
}
return firstChar;
}
int lastPos(string str, int start, int end)
{
int lastChar = -1;
for ( int i = start; i >= end; i--) {
if (str[i] >= 'a' && str[i] <= 'z' ) {
lastChar = i;
break ;
}
}
return lastChar;
}
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);
if (lastChar < 0 || firstChar < 0)
break ;
if (str[firstChar] == str[lastChar]) {
firstChar++;
lastChar--;
continue ;
}
ch = false ;
break ;
}
return (ch);
}
int main()
{
string str = "m a 343 la y a l am" ;
if (isPalindrome(str))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int firstPos(String str,
int start,
int end)
{
int firstChar = - 1 ;
for ( int i = start; i <= end; i++)
{
if (str.charAt(i) >= 'a' &&
str.charAt(i) <= 'z' )
{
firstChar = i;
break ;
}
}
return firstChar;
}
static int lastPos(String str,
int start,
int end)
{
int lastChar = - 1 ;
for ( int i = start; i >= end; i--)
{
if (str.charAt(i) >= 'a' &&
str.charAt(i) <= 'z' )
{
lastChar = i;
break ;
}
}
return lastChar;
}
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);
if (lastChar < 0 || firstChar < 0 )
break ;
if (str.charAt(firstChar) ==
str.charAt(lastChar))
{
firstChar++;
lastChar--;
continue ;
}
ch = false ;
break ;
}
return ch;
}
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" );
}
}
|
Python 3
def firstPos( str , start, end):
firstChar = - 1
for i in range (start, end + 1 ):
if ( str [i] > = 'a' and str [i] < = 'z' ) :
firstChar = i
break
return firstChar
def lastPos( str , start, end):
lastChar = - 1
for i in range (start, end - 1 , - 1 ) :
if ( str [i] > = 'a' and str [i] < = 'z' ) :
lastChar = i
break
return lastChar
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);
if (lastChar < 0 or firstChar < 0 ):
break
if ( str [firstChar] = = str [lastChar]):
firstChar + = 1
lastChar - = 1
continue
ch = False
break
return (ch)
if __name__ = = "__main__" :
str = "m a 343 la y a l am"
if (isPalindrome( str )):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static int firstPos( string str,
int start,
int end)
{
int firstChar = -1;
for ( int i = start; i <= end; i++)
{
if (str[i] >= 'a' &&
str[i] <= 'z' )
{
firstChar = i;
break ;
}
}
return firstChar;
}
static int lastPos( string str,
int start,
int end)
{
int lastChar = -1;
for ( int i = start; i >= end; i--)
{
if (str[i] >= 'a' &&
str[i] <= 'z' )
{
lastChar = i;
break ;
}
}
return lastChar;
}
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);
if (lastChar < 0 || firstChar < 0)
break ;
if (str[firstChar] ==
str[lastChar])
{
firstChar++;
lastChar--;
continue ;
}
ch = false ;
break ;
}
return ch;
}
public static void Main ()
{
string str = "m a 343 la y a l am" ;
if (isPalindrome(str))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function firstPos( $str , $start , $end )
{
$firstChar = -1;
for ( $i = $start ; $i <= $end ; $i ++)
{
if ( $str [ $i ] >= 'a' and
$str [ $i ] <= 'z' )
{
$firstChar = $i ;
break ;
}
}
return $firstChar ;
}
function lastPos( $str , $start , $end )
{
$lastChar = -1;
for ( $i = $start ; $i >= $end ; $i --)
{
if ( $str [ $i ] >= 'a' and
$str [ $i ] <= 'z' )
{
$lastChar = $i ;
break ;
}
}
return $lastChar ;
}
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 );
if ( $lastChar < 0 or $firstChar < 0)
break ;
if ( $str [ $firstChar ] == $str [ $lastChar ])
{
$firstChar ++;
$lastChar --;
continue ;
}
$ch = false;
break ;
}
return ( $ch );
}
$str = "m a 343 la y a l am" ;
if (isPalindrome( $str ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function firstPos(str,start,end)
{
let firstChar = -1;
for (let i = start; i <= end; i++)
{
if (str[i] >= 'a' &&
str[i] <= 'z' )
{
firstChar = i;
break ;
}
}
return firstChar;
}
function lastPos(str,start,end)
{
let lastChar = -1;
for (let i = start; i >= end; i--)
{
if (str[i] >= 'a' &&
str[i] <= 'z' )
{
lastChar = i;
break ;
}
}
return lastChar;
}
function isPalindrome(str)
{
let firstChar = 0,
lastChar = str.length - 1;
let ch = true ;
for (let i = 0; i < str.length; i++)
{
firstChar = firstPos(str, firstChar,
lastChar);
lastChar = lastPos(str, lastChar,
firstChar);
if (lastChar < 0 || firstChar < 0)
break ;
if (str[firstChar] ==
str[lastChar])
{
firstChar++;
lastChar--;
continue ;
}
ch = false ;
break ;
}
return ch;
}
let str = "m a 343 la y a l am" ;
if (isPalindrome(str))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time complexity: O(N2), where N is length of given string
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Nov, 2022
Like Article
Save Article